【Java】java Arrays中equals和deepEquals方法比较

java Arrays中equals和deepEquals方法

以下直接以代码解释


import java.util.Arrays;


public class Main{

    static int a[][]=new int[][] {{0},{1,2,3,5,4}};
    
    static int b[][]=new int[][] {{0},{1,2,3,5,4}};
    
    static int c[]=new int[] {1,2,3,5,4};
    
    static int d[]=new int[] {1,2,3,5,4};
    
    public static void main(String[] args) {
        
        //以下比较均对顺序敏感
        System.out.println(Arrays.equals(a[1],b[1]));//比较值,equals只适用于一层 true
        System.out.println(a.equals(b));//比较地址,false
        System.out.println(Arrays.deepEquals(a, b));//deepEquals适用于多层次,true
        
        System.out.println(c.equals(d));//比较地址false
        //System.out.println(Arrays.deepEquals(c, d));//报错:The method deepEquals(Object[], Object[]) in the type Arrays is not applicable for the arguments (int[], int[])
        
        String[] name1 = {"a","a","a","a","a","a"};
        String[] name2 = {"a","a","a","a","a","a"};
        System.out.println(Arrays.equals(name1, name2));      // true
        System.out.println(Arrays.deepEquals(name1, name2));  // true
    }
}

勇敢坚毅真正之才智乃刚毅之志向。 —— 拿破仑

你可能感兴趣的:(【Java】java Arrays中equals和deepEquals方法比较)