tutorials.de Buch-Aktion 05/2012
ERLEDIGT
JA
ANTWORTEN
4
ZUGRIFFE
917
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    -ben- -ben- ist offline Mitglied Silber
    Registriert seit
    Sep 2005
    Beiträge
    71
    hy!

    wieso erhalte ich bei folgendem code-snippet nicht den selben HashCode?
    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() );
    Wie kann ich dies umgehen?

    danke und gruss
    ben
     

  2. #2
    Registriert seit
    Jun 2002
    Ort
    Saarbrücken (Saarland)
    Beiträge
    9.886
    Blog-Einträge
    29
    Hallo!

    wieso erhalte ich bei folgendem code-snippet nicht den selben HashCode?
    Weil das zwei unterschiedliche Instanzen sind und die hashCode() Implementierung von java.lang.Object verwendet wird.

    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 Tom
     
    Java 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

  3. #3
    -ben- -ben- ist offline Mitglied Silber
    Registriert seit
    Sep 2005
    Beiträge
    71
    hy tom!

    ja, das dachte ich auch... jedoch dies erst ab java 1.5
    ich muss jedoch das jdk 1.4.2 einsetzen
     

  4. #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 Tom
     
    Java 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

  5. #5
    -ben- -ben- ist offline Mitglied Silber
    Registriert seit
    Sep 2005
    Beiträge
    71
    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

  1. Return eines Arrays
    Von realbora im Forum C/C++
    Antworten: 2
    Letzter Beitrag: 09.06.08, 16:12
  2. Filterung eines Arrays
    Von Paula im Forum PHP
    Antworten: 4
    Letzter Beitrag: 19.04.06, 13:38
  3. Konstruktor eines Arrays
    Von cibal_gina im Forum C/C++
    Antworten: 8
    Letzter Beitrag: 25.05.05, 16:43
  4. Speichern eines Arrays
    Von Despair Blue im Forum .NET Archiv
    Antworten: 4
    Letzter Beitrag: 12.04.05, 19:45
  5. Inhalt eines Arrays?
    Von Ruediger im Forum PHP
    Antworten: 2
    Letzter Beitrag: 10.07.03, 18:38