Sorry, it is very late so I may not explain all the details, but I have been working on this and I cannot understand why the object Item object reference in the array does not use the equals method of the Item class that it was given. I checked the class type of both Item objects inside the function and they return class Item.
import java.util.Arrays;
class Item{
private int data;
Item(int data){
this.data=data;
}
public boolean equals(Item item){
return data==item.data;
}
public String toString(){
return String.format("{data: %d}", data);
}
}
public class Problem3{
public static void main(String[] args){
Object object=new Object(){ public String toString(){return String.format("{hash code: %d}", hashCode());} };
String date="Friday, July 29";
Item item=new Item(2);
Object[] array={"Fri, Jul 29", new Item(2), object, new Integer[]{212, 220, 240, 313, 316, 320, 323, 331}, new Double[]{Math.E, Math.PI, 9.80665}, new Boolean[]{true, true, true, true}, new String[]{"Eckhart", "Eric", "Owen", "Chris", "David", "Mark"}};
System.out.println(Arrays.deepToString(array));
System.out.println();
System.out.println("Searching array for entries . . .");
System.out.printf("\"%s\": %b\n", date, isMember(array, date));
System.out.printf("%s: %b\n", item, isMember(array, item));
System.out.printf("%s: %b\n", object, isMember(array, object));
System.out.print("[\u0065, \u03c0, \u0047]: "+isMember(array, new Double[]{Math.E, Math.PI, 9.80665})); //\ud835 \u0065
}
private static boolean isMember(Object[] array, Object value){
if(array.length>0){
Object member=array[array.length-1];
if(member instanceof Object[] && value instanceof Object[]){
if(Arrays.deepEquals((Object[])member, (Object[])value)){
return true;
}
}
else if(member.getClass().equals(Item.class) && value.getClass().equals(Item.class)){
if(member.equals(value)){
return true;
}
}
else if(member.equals(value)){ //Object parameter does not have field "data" of Item equals method, so "instance of Item" applied above
return true;
}
Object[] arrayNext=Arrays.copyOf(array, array.length-1);
return isMember(arrayNext, value);
}
return false;
}
}