There are two ways to remove objects from ArrayList in Java, first, by using the remove() method, and second by using Iterator. ArrayList provides overloaded remove() method, one accepts the index of the object to be removed i.e. remove(int index), and the other accept objects to be removed, i.e. remove(Object obj).
Return values of remove() in JavaThe remove method returns true if an object passed as a parameter is removed from the list. Otherwise, it returns false. The remove method returns the removed element if an index is passed. It throws IndexOutOfBoundsException if the specified index is not in range.
The remove() method is used to remove an element at a specified index from ArrayList. Shifts any subsequent elements to the left (subtracts one from their indices). The index of the element to be removed.
This is part of my code.
Integer keyLocation = reducedFD.indexOf(KeyPlus.get(KEYindex)); someArrayList.remove(keyLocation);
So what I am doing here is I assign keyLocation(the first occurence of a string in the reducedFD arrayList). But when I want to remove from someArrayList the item with that keyLocation, it will not work.
If I input manually:
someArrayList.remove(0); //Let's say 0 is the actual keyLocation
This actually works.
What is weird is THAT THE FOLLOWING CODE ALSO WORKS:
someArrayList.remove(keyLocation + 1);
Any hints?
Here is the main loop:
for (int KEYindex = 0; KEYindex < KeyPlus.size(); KEYindex++){ Integer keyLocation = reducedFD.indexOf(KeyPlus.get(KEYindex)); if (reducedFD.contains(KeyPlus.get(KEYindex))){ KeyPlus.add(reducedFD.get(keyLocation+1)); CheckedAttributesPlus.add(KeyPlus.get(KEYindex)); reducedFD.remove(keyLocation); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With