java如何判断数组相等
时间: 2023-11-17 08:07:23 浏览: 115
java判断是否相等代码
在Java中,判断两个数组是否相等可以使用Arrays类的equals()方法。该方法会首先比较两个数组的长度是否相等,如果不相等则返回false,如果相等则按顺序比较两个数组中的元素是否完全相同。以下是一个示例代码:
```java
import java.util.Arrays;
public class ArrayEquality {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
int[] arr3 = {3, 2, 1};
boolean isEqual1 = Arrays.equals(arr1, arr2);
boolean isEqual2 = Arrays.equals(arr1, arr3);
System.out.println("arr1 and arr2 are equal? " + isEqual1);
System.out.println("arr1 and arr3 are equal? " + isEqual2);
}
}
```
输出:
```
arr1 and arr2 are equal? true
arr1 and arr3 are equal? false
```
阅读全文