Creating an instance of an object (o) and adding it to an Arraylist (arrayList) works fine. However, the remove function doesn't work.
arrayList.add(o); // works
arrayList.remove(o); // does nothing
What am I missing?
ArrayList.remove()
look like this:
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
So, if your Object
has default equals()
, then this cant work. All object are diffrent. Add equals()
to your Object
class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.