java中数组相等判断:
1.最常规的是遍历
public static boolean arrayEquals(String[] a,String[] b){ boolean flag = false; if(a.length != b.length){ return false; }else{ for (int i = 0; i < a.length; i++) { System.out.println(a[i] + " " + b[i]); if(!a[i].equals(b[i])){ flag = false; break; }else{ flag = true; } } } return flag; }
public static boolean arrayEquals(int[] a,int[] b){ boolean flag = false; if(a.length != b.length){ return false; } for(int i = 0; i < a.length; i++) { System.out.println(a[i] + " " + b[i]); if(a[i]!=b[i]){ flag = false; break; } flag = true; } return flag; }
2.用Arrays.equals
Arrays.equals(a, a2);
//下面是 Arrays.equals方法的源码 public static boolean equals(long[] a, long[] a2) { if (a==a2) return true; if (a==null || a2==null) return false; int length = a.length; if (a2.length != length) return false; for (int i=0; i<length; i++) if (a[i] != a2[i]) return false; return true; }