Java 中 hashCode(int[] a) 方法有什麼作用?


java.util.Arrays 類中的 hashCode(int[]) 方法基於指定陣列的內容返回雜湊碼。對於任意兩個非 null int 陣列 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) {
      int[] ival = new int[] { 3, 5 };
      int retval = ival.hashCode();
      System.out.println("The hash code of value1 is: " + retval);
      ival = new int[] { 19, 75 };
      retval = ival.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

更新日期: 2020 年 2 月 20 日

89 次瀏覽

開啟你的 事業

完成課程獲得認證

開始
廣告