I'm having some trouble with this for some reason.
I have two java Objects, both of which can be anything, including primitive and non-primitive arrays.
I need to perform an equals check.
If they are arrays, then I have to check their contents instead of their runtime instance.
So for example, let's say I have a method as such:
/**
* Returns true if the two parameters are arrays, and they both contain
* the same content.
* @param aObject1 An object
* @param aObject2 An object
* @return
*/
private boolean equalsArray( Object aObject1, Object aObject2 ) {
if(aObject1==null){
return false;
}
if(aObject2==null){
return false;
}
if(!aObject1.getClass().isArray()){
return false;
}
if(!aObject2.getClass().isArray()){
return false;
}
//How do I check if the two arrays here contain the same objects
//without knowledge of their type???
}
Note that the arrays can be anything, and will most likely not be Object[], but rather Foo[] or Bar[].
Any suggestions? I can't do Array.equals(Object[],Object[]) because I can't cast to Object[].
Arrays.equals((Object[]) aObject1, (Object[]) aObject2);is the way to go and you can of course castObjecttoObject[].