On Java 7's equals() and deepEquals()

前端 未结 4 1176
我在风中等你
我在风中等你 2021-02-19 03:39

Method description says:

Returns true if the arguments are deeply equal to each other and false otherwise... Equality is determined by using the equ

4条回答
  •  孤独总比滥情好
    2021-02-19 03:54

    deepEquals() is used with nested arrays of arbitrary depth.
    equals() is used with simple primitive data types.
    For ex:

    public class TwoDArray {
        public static void main(String args[]) {
            int a[][] = new int[2][2];
            int b[][] = new int[2][2];
            for(int i=0;i<2;i++)
                for(int j=0;j<2;j++) {
                    a[i][j] = i+j;
                    b[i][j] = i+j;  
                }
            System.out.println(Arrays.deepEquals(a,b));//return true
            System.out.println(Arrays.equals(a, b));//return false
        }
    }
    

提交回复
热议问题