Java中的Java .util.Arrays.equals() 和示例
今天我们将讨论检查两个数组是否相等的最简单方法。如果两个数组包含相同数量的元素,并且两个数组中所有对应的元素对相等,则认为两个数组相等。换句话说,如果两个数组以相同的顺序包含相同的元素,则它们是相等的。此外,如果两个数组引用都为空,则认为两个数组引用相等。 Java中的Arrays类提供了Arrays.equals()方法来检查两个数组是否相等。
Syntax :
public static boolean equals(int[] a, int[] a2)
Parameters :
a - one array to be tested for equality
a2 - the other array to be tested for equality
Returns :
true if the two arrays are equal
其他变体:
- 公共静态布尔等于(字节[] a,字节[] a2)
- 公共静态布尔等于(short[] a,short[] a2)
- 公共静态布尔等于(long [] a,long [] a2)
- 公共静态布尔等于(float[] a,float[] a2)
- 公共静态布尔等于(双[] a,双[] a2)
- 公共静态布尔等于(char[] a,char[] a2)
- 公共静态布尔等于(布尔[] a,布尔[] a2)
- 公共静态布尔等于(对象[] a,对象[] a2)
Java
// Java program to demonstrate working of Arrays.equals()
import java.util.Arrays;
public class ArrayEqualDemo
{
public static void main(String[] args)
{
// Let us create different integers arrays
int[] arr1 = new int [] {1, 2, 3, 4};
int[] arr2 = new int [] {1, 2, 3, 4};
int[] arr3 = new int [] {1, 2, 4, 3};
System.out.println("is arr1 equals to arr2 : " +
Arrays.equals(arr1, arr2));
System.out.println("is arr1 equals to arr3 : " +
Arrays.equals(arr1, arr3));
}
}
Java
// Java program to demonstrate working of Arrays.equals()
// for user defined objects.
import java.util.Arrays;
public class ArrayEqualDemo
{
public static void main (String[] args)
{
Student [] arr1 = {new Student(111, "bbbb", "london"),
new Student(131, "aaaa", "nyc"),
new Student(121, "cccc", "jaipur")};
Student [] arr2 = {new Student(111, "bbbb", "london"),
new Student(131, "aaaa", "nyc"),
new Student(121, "cccc", "jaipur")};
Student [] arr3 = {new Student(111, "bbbb", "london"),
new Student(121, "dddd", "jaipur"),
new Student(131, "aaaa", "nyc"),
};
System.out.println("is arr1 equals to arr2 : " +
Arrays.equals(arr1, arr2));
System.out.println("is arr1 equals to arr3 : " +
Arrays.equals(arr1, arr3));
}
}
// A class to represent a student.
class Student
{
int rollno;
String name, address;
// Constructor
public Student(int rollno, String name,
String address)
{
this.rollno = rollno;
this.name = name;
this.address = address;
}
@Override
public boolean equals(Object obj) {
// typecast obj to Student so that we can compare students
Student s = (Student) obj;
return this.rollno == s.rollno && this.name.equals(s.name)
&& this.address.equals(s.address);
}
}
Java
import java.util.Arrays;
public class ArrayEqualDemo_2 {
public static void main(String[] args)
{
// Let us create array of arrays
int[][] arr1 = { { 0, 1 }, { 1, 0 } };
int[][] arr2 = { { 0, 1 }, { 1, 0 } };
System.out.println("is arr1 equals to arr2 : "
+ Arrays.equals(arr1, arr2));
System.out.println("is arr1 deepequals to arr2 : "
+ Arrays.deepEquals(arr1, arr2));
}
}
输出:
is arr1 equals to arr2 : true
is arr1 equals to arr3 : false
我们还可以使用Arrays.equals()来检查用户定义类的对象数组的相等性。看看Arrays.equals()方法的最后一个变体
注意:-在对象数组的情况下,您必须重写 equals 方法以提供您自己的相等定义,否则您将获得输出取决于 Object 类的equals()方法返回的内容。在下面的程序中,我们正在检查学生的 rollno、姓名和地址是否相等。
Java
// Java program to demonstrate working of Arrays.equals()
// for user defined objects.
import java.util.Arrays;
public class ArrayEqualDemo
{
public static void main (String[] args)
{
Student [] arr1 = {new Student(111, "bbbb", "london"),
new Student(131, "aaaa", "nyc"),
new Student(121, "cccc", "jaipur")};
Student [] arr2 = {new Student(111, "bbbb", "london"),
new Student(131, "aaaa", "nyc"),
new Student(121, "cccc", "jaipur")};
Student [] arr3 = {new Student(111, "bbbb", "london"),
new Student(121, "dddd", "jaipur"),
new Student(131, "aaaa", "nyc"),
};
System.out.println("is arr1 equals to arr2 : " +
Arrays.equals(arr1, arr2));
System.out.println("is arr1 equals to arr3 : " +
Arrays.equals(arr1, arr3));
}
}
// A class to represent a student.
class Student
{
int rollno;
String name, address;
// Constructor
public Student(int rollno, String name,
String address)
{
this.rollno = rollno;
this.name = name;
this.address = address;
}
@Override
public boolean equals(Object obj) {
// typecast obj to Student so that we can compare students
Student s = (Student) obj;
return this.rollno == s.rollno && this.name.equals(s.name)
&& this.address.equals(s.address);
}
}
输出:
is arr1 equals to arr2 : true
is arr1 equals to arr3 : false
注意: String.equals()只能对一维数组执行,因此它不适用于多维数组。使用Arrays.deepEquals(Object[], Object[])代替,如果两个指定的数组彼此深度相等,则返回 true。
Java
import java.util.Arrays;
public class ArrayEqualDemo_2 {
public static void main(String[] args)
{
// Let us create array of arrays
int[][] arr1 = { { 0, 1 }, { 1, 0 } };
int[][] arr2 = { { 0, 1 }, { 1, 0 } };
System.out.println("is arr1 equals to arr2 : "
+ Arrays.equals(arr1, arr2));
System.out.println("is arr1 deepequals to arr2 : "
+ Arrays.deepEquals(arr1, arr2));
}
}
输出:
is arr1 equals to arr2 : false
is arr1 deepequals to arr2 : true