-2

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;
    }
}
Danny P.
  • 83
  • 6

3 Answers3

1

The equals method has the wrong signature and does therefore not override the Object.equals method. It should be

public boolean equals(Object item)
Henry
  • 42,982
  • 7
  • 68
  • 84
  • Okay sure, but then the Object object parameter of equals would not be able to resolve the variable Object.data, as it isn't a member or Object. How would I get an equals method that can compare fields of Item class? Item cast? – Danny P. Jul 30 '16 at 13:12
  • Yes, with a cast; after checking that the object is of the correct class. Return false if not. – Henry Jul 30 '16 at 14:28
  • One more thing... can two anonymous classes from new Object() be equal? public boolean equals(Object object){ if(object.getClass().equals(Item.class)){ return data==((Item)object).data; } return false; } Checking the instanceof the anonymous class argument and returning a flag for equal fields , similar to my code I am not sure would work. If this is possible, is it by getting the fields of each object and doing a deep equals? – Danny P. Jul 30 '16 at 16:11
  • Not sure I understand the question correctly, but to be able to cast to `Item`, the anonymous class must be derived from `Item` not from `Object`. – Henry Jul 30 '16 at 16:22
  • Two direct inheritants (anonymous inner classes) of Object type, having equal field values: could there be an equals or some other way to determine – Danny P. Jul 30 '16 at 20:08
  • Your equals method can do what it likes (as long as the general contract for equals is obeyed). But if you have several anonymous classes it becomes more and more awkward. Reflection could be a way to go. But this is really a different question. – Henry Jul 31 '16 at 05:20
1

You do not override equals from Object class. See here: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)

public boolean equals(Object obj)

To override a method in Java you have to use the same method signature. Your parameter is Item instead of Object.

swinkler
  • 1,703
  • 10
  • 20
1

Signature of equals method should be

public boolean equals(Object obj)
Divers
  • 9,531
  • 7
  • 45
  • 88