rreg101
rreg101

Reputation: 209

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;             
    }
}

Upvotes: 6

Views: 169

Answers (3)

Tmr
Tmr

Reputation: 278

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.

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;             
}

Upvotes: 1

Henry Zhu
Henry Zhu

Reputation: 2618

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;             
}

Upvotes: 2

keepmoving
keepmoving

Reputation: 2043

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));
       }
    }

Upvotes: 1

Related Questions