ERLEDIGT
JA
JA
ANTWORTEN
4
4
ZUGRIFFE
917
917
EMPFEHLEN
-
hy!
wieso erhalte ich bei folgendem code-snippet nicht den selben HashCode?
Wie kann ich dies umgehen?Code :1 2 3 4
int[] a1 = new int[]{1,2,3}; int[] a2 = new int[]{1,2,3}; System.out.println( a1.hashCode() ); System.out.println( a2.hashCode() );
danke und gruss
ben
-
04.12.05 17:45 #2
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo!
Weil das zwei unterschiedliche Instanzen sind und die hashCode() Implementierung von java.lang.Object verwendet wird.wieso erhalte ich bei folgendem code-snippet nicht den selben HashCode?
Wenn du fuer int Arrays mit gleichen Inhalten den gleichen hashCode moechtest kannst du das beispielsweise so machen:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/** * */ package de.tutorials; import java.util.Arrays; /** * @author Thomas * */ public class IntArrayHashCodeExample { /** * @param args */ public static void main(String[] args) { int[] a1 = new int[]{1,2,3}; int[] a2 = new int[]{1,2,3}; System.out.println(Arrays.hashCode(a1)); System.out.println(Arrays.hashCode(a2)); } }
Gruss TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
hy tom!
ja, das dachte ich auch... jedoch dies erst ab java 1.5
ich muss jedoch das jdk 1.4.2 einsetzen
-
04.12.05 18:04 #4
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo!
Na ja,
auf der einen Seite ist es ja kein Problem zu schauen wie da in Java 5 implementiert ist...:Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/** * Returns a hash code based on the contents of the specified array. * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt> * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. * * <p>The value returned by this method is the same value that would be * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} * method on a {@link List} containing a sequence of {@link Integer} * instances representing the elements of <tt>a</tt> in the same order. * If <tt>a</tt> is <tt>null</tt>, this method returns 0. * * @param a the array whose hash value to compute * @return a content-based hash code for <tt>a</tt> * @since 1.5 */ public static int hashCode(int a[]) { if (a == null) return 0; int result = 1; for (int element : a) result = 31 * result + element; return result; }
auf der anderen Seite gibts immernoch RetroWeaver mit dem fuer Java 5 entwickelte Anwendung auf Java 1.4 lauffaehig machen kann...
http://retroweaver.sourceforge.net/
Gruss TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
ja, ich habs jetzt auch so gelöst!
Retroweaver kannte ich bisher noch nicht. Danke für den Tipp, muss ich mir unbedingt mal angucken!
gruss
ben
Ähnliche Themen
-
Return eines Arrays
Von realbora im Forum C/C++Antworten: 2Letzter Beitrag: 09.06.08, 16:12 -
Filterung eines Arrays
Von Paula im Forum PHPAntworten: 4Letzter Beitrag: 19.04.06, 13:38 -
Konstruktor eines Arrays
Von cibal_gina im Forum C/C++Antworten: 8Letzter Beitrag: 25.05.05, 16:43 -
Speichern eines Arrays
Von Despair Blue im Forum .NET ArchivAntworten: 4Letzter Beitrag: 12.04.05, 19:45 -
Inhalt eines Arrays?
Von Ruediger im Forum PHPAntworten: 2Letzter Beitrag: 10.07.03, 18:38





Zitieren

Login





