equals methods when using arrays java -
for equals method checks see if 2 arrays equal, first method "equals" check if 2 arrays equal or tests memory addresses? or should 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; } }
the =
/!=
operator compares arrays based upon reference, , not content. 2 arrays may have same elements, except still 2 distinct objects created in memory. arrays 2 references. therefore second method should applied, because compares actual elements inside 2 arrays. don't need else
statement.
public static boolean equalarrays(double[] x, double[] y) { if(x.length != y.length) { return false; } (int index = 0; index < x.length; index++) { if (x[index] != y[index]) { return false; } } return true; }
Comments
Post a Comment