今天,我們將討論檢查兩個數組是否相等的最簡單方法。如果兩個數組包含相同數量的元素,並且兩個數組中所有對應的元素對均相等,則認為兩個數組相等。換句話說,如果兩個數組包含相同順序的相同元素,則它們相等。另外,如果兩個數組引用都為空,則認為它們相等。
Java中的Arrays類提供了Arrays.equals()方法來檢查兩個數組是否相等。
用法: public static boolean equals(int[] a, int[] a2) 參數: a - one array to be tested for equality a2 - the other array to be tested for equality 返回: true if the two arrays are equal
其他變體:
- 公共靜態布爾值equals(byte [] a,byte [] a2)
- 公共靜態布爾值equals(short [] a,short [] a2)
- 公共靜態布爾值equals(long [] a,long [] a2)
- 公共靜態布爾值equals(float [] a,float [] a2)
- 公共靜態布爾值equals(double [] a,double [] a2)
- 公共靜態布爾值equals(char [] a,char [] a2)
- 公共靜態布爾值等於(boolean [] a,boolean [] a2)
- 公共靜態布爾值equals(Object [] a,Object [] a2)
// 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));
}
}
輸出:
is arr1 equals to arr2:true is arr1 equals to arr3:false
我們也可以使用陣列.equals()用於檢查用戶定義的類的對象數組的相等性。陣列.equals()方法
注意:-如果是對象數組,則必須重寫equals方法以提供自己的相等性定義,否則,將根據Object類的equals()方法返回的內容獲得輸出。在下麵的程序中,我們將檢查學生的rollno,姓名和地址是否相等。
// 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
相關用法
- Java Java.util.function.IntPredicate用法及代碼示例
- Java Java.util.function.DoublePredicate用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java.util.concurrent.Phaser用法及代碼示例
- Java Java.util.function.LongPredicate用法及代碼示例
- Java Java.util.concurrent.RecursiveAction用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java.util.concurrent.RecursiveTask用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.util.Arrays.equals() in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。