等于使用数组java时的方法

时间:2022-07-02 13:24:20

For my equals method that checks to see if two arrays are equal, does the first method "equals" actually check if the two arrays are equal or only tests the memory addresses? Or should I include both?

对于检查两个数组是否相等的我的equals方法,第一个方法“equals”是否实际检查两个数组是否相等还是仅测试内存地址?或者我应该包括两者?

   public boolean equals(Object otherObject)
    {
       if (otherObject == null)
       {
           return false;
       }
       else if (getClass() != otherObject.getClass())
       {
           return false;
       }
       else
       {
          RegressionModel otherRegressionModel = (RegressionModel)otherObject;
          return (xValues == (otherRegressionModel.xValues) && yValues == (otherRegressionModel.yValues));
       }
    }

OR

public static boolean equalArrays(double[] x, double[] y)
{
    if(x.length != y.length)
    {
        return false;
    }
    else
    {
        for(int index = 0; index < x.length; index++)
        {
            if (x[index] != y[index])
            {
                return false;
            }
        }
        return true;             
    }
}

3 个解决方案

#1


2  

the =/!= operator compares arrays based upon their reference, and not their content. Clearly two arrays may have the same elements, except they are still two distinct objects that are created in the memory. The arrays are two references. Therefore your second method should be applied, because it compares the actual elements inside the two arrays. Also you don't need your else statement.

= /!=运算符根据数组的引用而不是内容来比较数组。显然,两个数组可能具有相同的元素,除了它们仍然是在内存中创建的两个不同的对象。数组是两个引用。因此,应该应用第二种方法,因为它比较了两个数组中的实际元素。你也不需要你的else语句。

public static boolean equalArrays(double[] x, double[] y)
{
    if(x.length != y.length)
    {
        return false;
    }
    for (int index = 0; index < x.length; index++)
    {
        if (x[index] != y[index])
        {
            return false;
        }
    }
    return true;             
}

#2


1  

One more check can also be applied on first equals method where it can see both array has an same reference or not. Then other comparison can be done. In second method it always check the element of an array, even if both an reference of same array. so in terms of performance it will take time.

还可以对第一个equals方法应用另一个检查,其中可以看到两个数组具有相同的引用。然后可以进行其他比较。在第二种方法中,它总是检查数组的元素,即使两个引用都是相同的数组。所以在性能方面需要时间。

public boolean equals(Object otherObject)
    {
       if (otherObject == null)
       {
           return false;
       }
       else if (getClass() != otherObject.getClass())
       {
           return false;
       } else if (this != otherObject)
          return false;
       }
       else
       {
          RegressionModel otherRegressionModel =                        (RegressionModel)otherObject;
          return (xValues == (otherRegressionModel.xValues) && yValues == (otherRegressionModel.yValues));
       }
    }

#3


1  

Adding to @Henry's answer, before comparing lengths of the two given arrays you should make sure that neither of them are null so that you don't get a NullPointerException.

添加@ Henry的答案,在比较两个给定数组的长度之前,你应该确保它们都不为null,这样你就不会得到NullPointerException。

You might also want to compare the references of the arrays before looping through their elements.

您可能还希望在循环遍历其元素之前比较数组的引用。

Something like this:

像这样的东西:

public static boolean equalArrays(double[] x, double[] y)
{
    if (x == null || y == null || x.length != y.length)
    {
        //NOTE: That if both x and y are null we return false despite the fact that you could argue that they are equal
        return false;
    }
    if (x == y)
    {
        return true;
    }
    for (int index = 0; index < x.length; index++)
    {
        if (x[index] != y[index])
        {
            return false;
        }
    }
    return true;             
}

#1


2  

the =/!= operator compares arrays based upon their reference, and not their content. Clearly two arrays may have the same elements, except they are still two distinct objects that are created in the memory. The arrays are two references. Therefore your second method should be applied, because it compares the actual elements inside the two arrays. Also you don't need your else statement.

= /!=运算符根据数组的引用而不是内容来比较数组。显然,两个数组可能具有相同的元素,除了它们仍然是在内存中创建的两个不同的对象。数组是两个引用。因此,应该应用第二种方法,因为它比较了两个数组中的实际元素。你也不需要你的else语句。

public static boolean equalArrays(double[] x, double[] y)
{
    if(x.length != y.length)
    {
        return false;
    }
    for (int index = 0; index < x.length; index++)
    {
        if (x[index] != y[index])
        {
            return false;
        }
    }
    return true;             
}

#2


1  

One more check can also be applied on first equals method where it can see both array has an same reference or not. Then other comparison can be done. In second method it always check the element of an array, even if both an reference of same array. so in terms of performance it will take time.

还可以对第一个equals方法应用另一个检查,其中可以看到两个数组具有相同的引用。然后可以进行其他比较。在第二种方法中,它总是检查数组的元素,即使两个引用都是相同的数组。所以在性能方面需要时间。

public boolean equals(Object otherObject)
    {
       if (otherObject == null)
       {
           return false;
       }
       else if (getClass() != otherObject.getClass())
       {
           return false;
       } else if (this != otherObject)
          return false;
       }
       else
       {
          RegressionModel otherRegressionModel =                        (RegressionModel)otherObject;
          return (xValues == (otherRegressionModel.xValues) && yValues == (otherRegressionModel.yValues));
       }
    }

#3


1  

Adding to @Henry's answer, before comparing lengths of the two given arrays you should make sure that neither of them are null so that you don't get a NullPointerException.

添加@ Henry的答案,在比较两个给定数组的长度之前,你应该确保它们都不为null,这样你就不会得到NullPointerException。

You might also want to compare the references of the arrays before looping through their elements.

您可能还希望在循环遍历其元素之前比较数组的引用。

Something like this:

像这样的东西:

public static boolean equalArrays(double[] x, double[] y)
{
    if (x == null || y == null || x.length != y.length)
    {
        //NOTE: That if both x and y are null we return false despite the fact that you could argue that they are equal
        return false;
    }
    if (x == y)
    {
        return true;
    }
    for (int index = 0; index < x.length; index++)
    {
        if (x[index] != y[index])
        {
            return false;
        }
    }
    return true;             
}