在Java中比较数组元素有两种方式:
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = {1, 2, 3, 4, 5};
boolean isEqual = Arrays.equals(array1, array2);
if(isEqual) {
System.out.println("Arrays are equal");
} else {
System.out.println("Arrays are not equal");
}
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = {1, 2, 3, 4, 6};
boolean isEqual = true;
if(array1.length != array2.length) {
isEqual = false;
} else {
for(int i=0; i<array1.length; i++) {
if(array1[i] != array2[i]) {
isEqual = false;
break;
}
}
}
if(isEqual) {
System.out.println("Arrays are equal");
} else {
System.out.println("Arrays are not equal");
}