Java的满足Arrays.equals()的二维数组返回false [英] Java Arrays.equals() returns false for two dimensional arrays

查看:561
本文介绍了Java的满足Arrays.equals()的二维数组返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇,想知道 - 为什么满足Arrays.equals(双[] [],双击[] [])返回假的?而实际上阵列具有相同数量的元素,每个元素是一样的吗?

例如我进行下面的测试。

 双击[] [] A,B;
INT大小= 5;A =新的双[尺寸] [SIZE]
B =新的双[尺寸] [SIZE]的for(int i = 0; I<大小;我++)
    对于(INT J = 0; J<大小; J ++){
        一个[I] [J] = 1.0;
        B〔I] [J] = 1.0;
    }如果(满足Arrays.equals(a,b))的
    的System.out.println(平等);
其他
    的System.out.println(不等于);

返回false,并打印不等于。

在另一方面,如果我有这样的事情:

 双击[] A,B;
INT大小= 5;A =新的双[尺寸]
B =新的双[尺寸]的for(int i = 0; I<大小;我++){
    一个由[i] = 1.0;
    B〔Ⅰ〕= 1.0;
}如果(满足Arrays.equals(a,b))的
    的System.out.println(平等);
其他
    的System.out.println(不等于);

返回true,并打印平等。请问方法仅采用单维度的工作原理?如果是的话,是有多维数组类似的东西在Java中?


解决方案

使用<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#deepEquals%28java.lang.Object%5B%5D,%20java.lang.Object%5B%5D%29\"><$c$c>deepEquals(Object[],对象[]) 。


  

返回真正如果两个指定数组的深层相等的彼此。


由于一个 INT [] 的instanceof对象,一个 INT [] [ ] 的instanceof对象[]


至于为什么满足Arrays.equals 不工作的二维数组,它可以通过一步解释如下步骤:

对于数组,等于在对象标识来定义

 的System.out.println(
    (新INT [] {1,2})。等于(新INT [] {1,2})
); //输出假

这是因为数组继承其等于从他们的公共超,<一个href=\"http://java.sun.com/javase/6/docs/api/java/lang/Object.html#equals%28java.lang.Object%29\"><$c$c>Object.

通常我们真的想为数组值相等,当然,这也就是为什么 java.util.Arrays中的提供了静态实用方法等于(INT [],INT [])

 的System.out.println(
    java.util.Arrays.equals(
        新的INT [] {1,2},
        新的INT [] {1,2}
    )
); //输出真

在Java中数组的数组


  • INT [] 的instanceof对象

  • INT [] [] 的instanceof对象[]

  • INT [] [] 的instanceof INT []

Java不真的有二维数组。它甚至没有真正拥有多维数组。 Java有数组的数组。

java.util.Arrays.equals 是浅

现在考虑这个片断:

 的System.out.println(
    java.util.Arrays.equals(
        新的INT [] [] {
            {1},
            {2,3},
        },
        新的INT [] [] {
            {1},
            {2,3},
        }
    )
); //输出假

下面是事实:


  • 每个参数是一个对象[]

    • 在索引为0的元素是 INT [] {1}

    • 在索引1的元素是 INT [] {2,3}


  • 有两个对象[] 实例

  • 有四个 INT [] 实例

有应该从previous点,这将调用<清晰href=\"http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#equals%28java.lang.Object%5B%5D,%20java.lang.Object%5B%5D%29\"><$c$c>Arrays.equals(Object[],对象[]) 超载​​。从API:


  

返回真正如果对象两个指定数组彼此相等。这两个数组被认为是等于如果两个数组包含相同数量的元素,并且两个数组中元素的所有相应对都相等。两个物体 E1 E2 如果(E1 == NULL?E2被视为相等==空:e1.equals(E2))


现在应该很清楚为什么上面的片断输出;这是因为对象的元素[] 数组不是由上述定义相等(因为一个 INT [] 有其等于按对象的身份定义)。

java.util.Arrays.deepEquals 是深

在此相反,这里是<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#deepEquals%28java.lang.Object%5B%5D,%20java.lang.Object%5B%5D%29\"><$c$c>Arrays.deepEquals(Object[],对象[]) 做:


  

返回真正如果两个指定数组的深受的彼此相等。不同于等于(Object []对象,对象[])的方法,此方法适用于任意深度的嵌套数组。


 的System.out.println(
    java.util.Arrays.deepEquals(
        新的INT [] [] {
            {1},
            {2,3},
        },
        新的INT [] [] {
            {1},
            {2,3},
        }
    )
); //输出真


Arrays.toString Arrays.deepToString

值得注意的是这两种方法,我们已经与问候嵌套数组到目前为止讨论。之间有什么比喻

 的System.out.println(
    java.util.Arrays.toString(
        新的INT [] [] {
            {1},
            {2,3},
        }
    )
); //输出[我@ 187aeca,[I @ e48e1b]的System.out.println(
    java.util.Arrays.deepToString(
        新的INT [] [] {
            {1},
            {2,3},
        }
    )
); //输出[[1],[2,3]]

再次推理是相似的: Arrays.toString(对象[])将每个元素作为对象 ,并调用它的的toString()方法。数组继承其的toString()从他们的公共超对象

如果你想 java.util.Arrays中的来考虑嵌套数组,你需要使用 deepToString ,就像你需要使用 deepEquals

I was just curious to know - why does Arrays.equals(double[][], double[][]) return false? when in fact the arrays have the same number of elements and each element is the same?

For example I performed the following test.

double[][] a,  b;
int size =5;

a=new double[size][size];
b=new double[size][size];

for( int i = 0; i < size; i++ )
    for( int j = 0; j < size; j++ ) {
        a[i][j]=1.0;
        b[i][j]=1.0;
    }

if(Arrays.equals(a, b))
    System.out.println("Equal");
else
    System.out.println("Not-equal");

Returns false and prints "Not-equal".

on the other hand, if I have something like this:

double[] a,  b;
int size =5;

a=new double[size];
b=new double[size];

for( int i = 0; i < size; i++ ){
    a[i]=1.0;
    b[i]=1.0;
} 

if(Arrays.equals(a, b))
    System.out.println("Equal");
else
    System.out.println("Not-equal");

returns true and prints "Equal". Does the method only work with single dimensions? if so, is there something similar for multi-dimensional arrays in Java?

解决方案

Use deepEquals(Object[], Object[]).

Returns true if the two specified arrays are deeply equal to one another.

Since an int[] is an instanceof Object, an int[][] is an instanceof Object[].


As to why Arrays.equals doesn't "work" for two dimensional arrays, it can be explained step by step as follows:

For arrays, equals is defined in terms of object identity

System.out.println(
    (new int[] {1,2}).equals(new int[] {1,2})
); // prints "false"

This is because arrays inherit their equals from their common superclass, Object.

Often we really want value equality for arrays, of course, which is why java.util.Arrays provides the static utility method equals(int[], int[]).

System.out.println(
    java.util.Arrays.equals(
        new int[] {1,2},
        new int[] {1,2}
    )
); // prints "true"

Array of arrays in Java

  • An int[] is an instanceof Object
  • An int[][] is an instanceof Object[]
  • An int[][] is NOT an instanceof int[]

Java doesn't really have two dimensional arrays. It doesn't even really have multidimensional arrays. Java has array of arrays.

java.util.Arrays.equals is "shallow"

Now consider this snippet:

System.out.println(
    java.util.Arrays.equals(
        new int[][] {
            { 1 },
            { 2, 3 },
        },
        new int[][] {
            { 1 },
            { 2, 3 },
        }
    )
); // prints "false"

Here are the facts:

  • Each argument is an Object[]
    • The element at index 0 is an int[] { 1 }
    • The element at index 1 is an int[] { 2, 3 }.
  • There are two Object[] instances
  • There are four int[] instances

It should be clear from the previous point that this invokes Arrays.equals(Object[], Object[]) overload. From the API:

Returns true if the two specified arrays of Objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)).

Now it should be clear why the above snippet prints "false"; it's because the elements of the Object[] arrays are not equal by the above definition (since an int[] has its equals defined by object identity).

java.util.Arrays.deepEquals is "deep"

In contrast, here's what Arrays.deepEquals(Object[], Object[]) does:

Returns true if the two specified arrays are deeply equal to one another. Unlike the equals(Object[],Object[]) method, this method is appropriate for use with nested arrays of arbitrary depth.

System.out.println(
    java.util.Arrays.deepEquals(
        new int[][] {
            { 1 },
            { 2, 3 },
        },
        new int[][] {
            { 1 },
            { 2, 3 },
        }
    )
); // prints "true"


On Arrays.toString and Arrays.deepToString

It's worth noting the analogy between these two methods and what we've discussed so far with regards to nested arrays.

System.out.println(
    java.util.Arrays.toString(
        new int[][] {
            { 1 },
            { 2, 3 },
        }
    )
); // prints "[[I@187aeca, [I@e48e1b]"

System.out.println(
    java.util.Arrays.deepToString(
        new int[][] {
            { 1 },
            { 2, 3 },
        }
    )
); // prints "[[1], [2, 3]]"

Again, the reasoning is similar: Arrays.toString(Object[]) treats each element as an Object, and just call its toString() method. Arrays inherit its toString() from their common superclass Object.

If you want java.util.Arrays to consider nested arrays, you need to use deepToString, just like you need to use deepEquals.

这篇关于Java的满足Arrays.equals()的二维数组返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆