I am coding in Java and I understand well that I can compare most arrays with Arrays.equals
, and I can compare strings case insensitively with String.equalsIgnoreCase
, but is there a way to specifically compare Arrays of strings case insensitively?
sample code:
class Foo {
String[] array; // don't care about case
@Override
public boolean equals(Foo obj) {
// bunch of stuff to check equality
if (!Arrays.equals(array, obj.array)) return false; // case insensitive?
return true;
}
}
Your code wont compile as you are not overriding the Object's equals method which has signature like public boolean equals(Object obj)
Also to do comparison, you could do the following in your modified equals as below:
if (array == foo.array) {
return true;
}
if (array == null || foo.array == null) {
return false;
}
int length = array.length;
if (foo.length != length)
return false;
for (int i=0; i<length; i++) {
String string1 = array[i];
String string2 = foo.array[i];
if (!(string1==null ? string2==null : string1.equalsIgnoreCase(string2)))
return false;
}
return true;