如何比较两个数组?

时间:2022-12-13 23:05:49

How do I compare two arrays, in size AND in content?

如何比较两个数组的大小和内容?

In the class with the constructor I created this equals() method:

在使用构造函数的类中,我创建了这个equals()方法:

public boolean equals(VarArray otherArray) {
    if ((myInts.equals(otherArray)))) { 
        return true;
    }else {
        return false;
    }
}

This is how I have it in the class I'm testing with and I still get false instead of true:

在我正在测试的课程中,我就是这样做的,但我还是得到了假的而不是真的:

int[] array = {0,1,2,3,4,5,6,7,8,9,10};

VarArray testArray = new VarArray(array);
VarArray testArray2 = new VarArray(array);

System.out.println("\n" + testArray.equals(testArray2)); // should be true

4 个解决方案

#1


0  

otherArray is of type VarArray and you're comparing it to an array of ints. What you want is:

otherArray类型为VarArray,您将它与一个ints数组进行比较。你想要的是:

public boolean equals(VarArray otherArray) {
   return Arrays.equals(myInts, otherArray.myInts);
}

#2


4  

use Arrays.equals(testArray, testArray2);

使用数组。=(testArray testArray2);

#3


0  

Arrays.deepEquals() will perform deep comparison, which will also check for any nested elements.

deepequals()将执行深度比较,它还将检查任何嵌套元素。

public boolean equals(VarArray otherArray) {
    return Arrays.deepEquals(myInts, otherArray.getInts());
}

#4


0  

Your implmentaion of equals is wrong. In your case it will call the equals method of array which will compare the object instance of both the array which are different. Correct implementation of equals method will look like this:

你对平等的恳求是错误的。在你的例子中,它将调用数组的equals方法,它将比较数组的对象实例和不同的对象实例。正确的equals方法实现如下:

public class VarArray
{

    private int[] myInts = null;

    private int[] getMyInts()
    {
        return myInts;
    }

    public VarArray(int[] arr)
    {
        myInts = arr;
    }

    public boolean equals(VarArray otherArray)
    {
        if (myInts == null && otherArray == null)
        {
            return true;
        }

        if (myInts == null || otherArray == null)
        {
            return false;
        }

        if (myInts.length != otherArray.getMyInts().length)
        {
            return false;
        }

        int[] otherMyInts = otherArray.getMyInts();
        for (int i = 0; i < myInts.length; i++)
        {
            if (myInts[i] != otherMyInts[i])
            {
                return false;
            }
        }

        return true;
    }
}

or if you don't want to write such code, which is in my opinion waste of time and energy use java.util.Arrays and use this code

或者如果您不想编写这样的代码,在我看来这是浪费时间和精力的使用java.util。数组并使用此代码

int[] array = {0,1,2,3,4,5,6,7,8,9,10};
        int[] array1 = {0,1,2,3,4,5,6,7,8,9,10};
boolean isEqual = Arrays.equals(array, array1);
        System.out.println(isEqual);

#1


0  

otherArray is of type VarArray and you're comparing it to an array of ints. What you want is:

otherArray类型为VarArray,您将它与一个ints数组进行比较。你想要的是:

public boolean equals(VarArray otherArray) {
   return Arrays.equals(myInts, otherArray.myInts);
}

#2


4  

use Arrays.equals(testArray, testArray2);

使用数组。=(testArray testArray2);

#3


0  

Arrays.deepEquals() will perform deep comparison, which will also check for any nested elements.

deepequals()将执行深度比较,它还将检查任何嵌套元素。

public boolean equals(VarArray otherArray) {
    return Arrays.deepEquals(myInts, otherArray.getInts());
}

#4


0  

Your implmentaion of equals is wrong. In your case it will call the equals method of array which will compare the object instance of both the array which are different. Correct implementation of equals method will look like this:

你对平等的恳求是错误的。在你的例子中,它将调用数组的equals方法,它将比较数组的对象实例和不同的对象实例。正确的equals方法实现如下:

public class VarArray
{

    private int[] myInts = null;

    private int[] getMyInts()
    {
        return myInts;
    }

    public VarArray(int[] arr)
    {
        myInts = arr;
    }

    public boolean equals(VarArray otherArray)
    {
        if (myInts == null && otherArray == null)
        {
            return true;
        }

        if (myInts == null || otherArray == null)
        {
            return false;
        }

        if (myInts.length != otherArray.getMyInts().length)
        {
            return false;
        }

        int[] otherMyInts = otherArray.getMyInts();
        for (int i = 0; i < myInts.length; i++)
        {
            if (myInts[i] != otherMyInts[i])
            {
                return false;
            }
        }

        return true;
    }
}

or if you don't want to write such code, which is in my opinion waste of time and energy use java.util.Arrays and use this code

或者如果您不想编写这样的代码,在我看来这是浪费时间和精力的使用java.util。数组并使用此代码

int[] array = {0,1,2,3,4,5,6,7,8,9,10};
        int[] array1 = {0,1,2,3,4,5,6,7,8,9,10};
boolean isEqual = Arrays.equals(array, array1);
        System.out.println(isEqual);