0

Equals method recursion

How can I implement an equals method in java recursively?

7th Dec 2021, 3:00 AM
snaj
2 Respostas
+ 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) ); } }
10th Dec 2021, 9:58 PM
zemiak
0
Why do you want to implement equals recursively?
7th Dec 2021, 5:35 AM
Kamen Studentov
Kamen Studentov - avatar