hashCode(obj[] a) 方法在 Java 中做什麼?


java.util.Arrays 類的 hashCode(Object[]) 方法基於指定陣列的內容返回雜湊碼。如果陣列包含其他元素陣列,那麼雜湊碼基於它們的標識,而不是它們的內容。對於任意兩個陣列 a 和 b,若 Arrays.equals(a, b) 成立,那麼 Arrays.hashCode(a) == Arrays.hashCode(b) 也成立。

示例

import java.util.Arrays;
   public class ArrayDemo {   
      public static void main(String[] args) {
      Object[] ob = new Object[] { 22, 7 };
      int retval = ob.hashCode();
      System.out.println("The hash code of value1 is: " + retval);
      
      ob = new Object[] { 3.5, 8.5 };
      retval = ob.hashCode();
      System.out.println("The hash code of value2 is: " + retval);
   }
}

輸出

The hash code of value1 is: 4072869
The hash code of value2 is: 1671711

更新時間: 2019 年 7 月 30 日

99 次瀏覽

啟動你的 職業

完成課程獲得認證

開始
廣告