6

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;             
    }
}
3
  • 7
    Or Arrays.equals...? Commented May 12, 2015 at 0:15
  • @MadProgrammer Yes but I want to override it and create it myself Commented May 12, 2015 at 0:21
  • @rreg101 That's ok, but perhaps you should could start by looking at the source code of the method that MadProgrammer suggested. Commented May 13, 2015 at 14:05

3 Answers 3

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.

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;             
}
Sign up to request clarification or add additional context in comments.

Comments

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.

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

Comments

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.