I’m trying to remove an object from an ArrayList. Each Item object has 3 attributes; 1. itemNum 2. info 3. cost. I also have 3 classes, 1. Item class defines the single item
Override equals method in your Item class. You can use itemNum to check the equality of objects in your equals method.
Then use ArrayList remove(Object o) method to delete the object. The remove method uses equals internally to find the object to be removed.
EDIT:
You are not overriding the equals method properly, here is the right signature and implementation:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Item other = (Item) obj;
if (itemNum != other.itemNum)
return false;
return true;
}