我们可以使用增强的 for 循环而不会出现 ArrayIndexOutOfBound 错误。因为在使用正常的 for 循环后它正在工作。
public static void main(String[] args) {
int a[] = {1,2,3,4};
int b[] = {1,2,3,4};
boolean status = true;
if (a.length == b.length){
for (int i:a){
if (a[i] != b[i]){
status =false;
}
}
}
else {
status = false;
}
if (status == true){
System.out.println("arrays are equal...");
}
else {
System.out.println("arrays not equal...");
}
}
}