1

So I have to do as a homework in my class, to implement my own version of a Deque supported on an array, which can be accessed by both the tail or the head. My teacher provided us a JUnit test class to check if all our methods work. Two of those are clone() and equals(), I need this last one for the test class to check if two cloned ArrayDeque are the same. I just need help with this because it won't work with the next code:

@Override
public boolean equals(Object obj) 
{
    for (Object elem : items)
    {
        E x = (E) elem;
        if (!x.equals((E)obj)) 
        {
            return false;
        }
    }
    return true;
}

It would really help me if someone could give me a hand with this...

Thank you very much

EDIT: items is an attribute with the array of elements of the deque EDIT2:

Is this okay then?

public boolean equals(Object obj) 
{
    TSBDeQueue object = (TSBDeQueue) obj;
    for (int i = 0; i < items.length; i++)
    {
        if (!object.items[i].equals(this.items[i]))
        {
            return false;
        }
    }
    return true;
}
Manu Carba
  • 139
  • 2
  • 8
  • You don't need the cast to call `equals()`. – shmosel Oct 17 '17 at 02:43
  • If `obj` is another `ArrayDeque`, what do you think `x.equals(obj)` will return? – shmosel Oct 17 '17 at 02:43
  • @shmosel this is what I don't understand, specially what I'm suposed to get as a param – Manu Carba Oct 17 '17 at 02:49
  • 1
    You need to iterate over `this` and `obj` in parallel, and return when you find a mismatch. – shmosel Oct 17 '17 at 02:50
  • see https://stackoverflow.com/questions/2989987/how-can-i-check-if-two-arraylist-differ-i-dont-care-whats-changed – Scary Wombat Oct 17 '17 at 02:53
  • Won't let me because obj is only one attribute, it has no [ ] at all so the compiler won't let me iterate over it. – Manu Carba Oct 17 '17 at 02:53
  • I don't know what that means. You're already iterating over `this` with your `for` loop. If `items` is an array, just loop over both with an index instead. – shmosel Oct 17 '17 at 02:55
  • You need to cast `obj` to your `ArrayDeque` and then you can iterate over it's elements. – Oleg Oct 17 '17 at 02:57
  • What about now @shmosel ? – Manu Carba Oct 17 '17 at 03:02
  • Better. But you've overlooked the possibility of `object.items` being shorter than `this.items`. – shmosel Oct 17 '17 at 03:03
  • @shmosel Test Class launches NullPointerException: http://prntscr.com/gyc0xy – Manu Carba Oct 17 '17 at 03:09
  • You're not checking if `obj` is null (or if `items` is null, assuming that's possible). – shmosel Oct 17 '17 at 03:12
  • You're also casting without verifying that `obj` is, in fact, a `TSBDeQueue`. – shmosel Oct 17 '17 at 03:12
  • I don't know how to verify that it is a TSBDeQueue. Anyways, what does it have to do with a NullPointerException? I'm not sure if that may be the reason of that error – Manu Carba Oct 17 '17 at 03:18
  • `a.equals(b)` throws a `NullPointerException` if `a` is null. Use `Objects.equals(a, b)` instead (or `(a == null) ? (b == null) : a.equals(b)`). Also, `object.items[i].equals(this.items[i])` throws an NPE if `object` is null; you can't work around that, other than by testing `object == null` explicitly. – Andy Turner Oct 17 '17 at 03:23
  • Or, rather than using the loop explicitly, you can use `Arrays.equals(object.items[i], this.items[i])`. – Andy Turner Oct 17 '17 at 03:26

0 Answers0