Java數組比較
如何檢查兩個數組是否相等?
解決方法
下麵的示例演示如何使用數組的equals()方法來檢查兩個數組相等與否。
import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { int[] ary = {1,2,3,4,5,6}; int[] ary1 = {1,2,3,4,5,6}; int[] ary2 = {1,2,3,4}; System.out.println("Is array 1 equal to array 2?? " +Arrays.equals(ary, ary1)); System.out.println("Is array 1 equal to array 3?? " +Arrays.equals(ary, ary2)); } }
結果
上麵的代碼示例將產生以下結果。
Is array 1 equal to array 2?? true Is array 1 equal to array 3?? false