I have MyClass
, which stores two integers, and I have a Vector<MyClass>
called myVector
.
Now, I do this:
...
myVector.add(new MyClass(1,1));
for(MyClass m : myVector)
System.out.println(m);
System.out.println(myVector.size());
myVector.remove(new MyClass(1,1));
for(MyClass m : myVector)
System.out.println(m);
System.out.println(myVector.size());
...
The problem here is that the object isn't being removed, as I see when I print the Vector and its size. How could I fix that?
Edit: I can see that it isn't finding the object and I've tested with contains()
to be sure. What I need is to compare it by value. If I could overload the ==
operator I could do it, but I have no idea how to fix this.
Edit 2: Okay, equals()
will do what I want it to. But I'm not sure what to put in the hashCode method.
Edit 3: I can find it with contains(), but remove doesn't remove it.
I can use this to remove it though:
int position = myVector.indexOf(new MyClass(1,1));
myVector.remove(position);
Which is the same as remove(new MyClass(1,1), except the above code works and remove() doesn't. Any thoughts?