当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.util.Arrays.equals()用法及代码示例


今天,我们将讨论检查两个数组是否相等的最简单方法。如果两个数组包含相同数量的元素,并且两个数组中所有对应的元素对均相等,则认为两个数组相等。换句话说,如果两个数组包含相同顺序的相同元素,则它们相等。另外,如果两个数组引用都为空,则认为它们相等。

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.util.Arrays.equals() in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。