0
Equals method recursion
How can I implement an equals method in java recursively?
2 Respuestas
+ 1
class ArrayWrap {
int[] a;
ArrayWrap(int[] a) {
java.util.Objects.requireNonNull(a);
this.a = a;
}
public boolean equals( ArrayWrap other) {
return this == other
|| a.length == other.a.length
&& equalsHelper(other,0);
}
private boolean equalsHelper( ArrayWrap p, int idx) {
if (idx == a.length)
return true;
return equalsHelper(p,idx+1)
&& a[idx] == p.a[idx];
}
public static void main(String[] args) {
int[] a = new int[]{1,2,3,4,5};
int[] b = new int[]{1,2,3,4,5};
ArrayWrap wrapA = new ArrayWrap(a);
ArrayWrap wrapB = new ArrayWrap(b);
System.out.println( wrapA.equals( wrapB) );
}
}
0
Why do you want to implement equals recursively?