Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equals methods when using arrays java

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?

   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;             
    }
}
like image 209
rreg101 Avatar asked May 12 '15 00:05

rreg101


People also ask

Can we use equals method for array in Java?

equals() Method. Java Arrays class provides the equals() method to compare two arrays. It iterates over each value of an array and compares the elements using the equals() method.

What does arrays equals do in Java?

equals(Object[] a, Object[] a2) method 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.

How do you make two arrays equal in Java?

In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null. Arrays class in java provide the method Arrays. equals() to check whether two arrays are equal or not.

Do not use the Object equals () method to compare two arrays?

Because the effect of using Object. equals() to compare two arrays is often misconstrued as content equality, and because a better alternative exists in the use of reference equality operators, the use of the Object. equals() method to compare two arrays is disallowed.


2 Answers

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.

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;             
}
like image 72
Henry Zhu Avatar answered Oct 04 '22 17:10

Henry Zhu


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.

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));
       }
    }
like image 24
Still Learning Avatar answered Oct 04 '22 16:10

Still Learning