Remove Item from ArrayList
Solution 1:
In this specific case, you should remove the elements in descending order. First index 5
, then 3
, then 1
. This will remove the elements from the list without undesirable side effects.
for (int j = i.length-1; j >= 0; j--) {
list.remove(i[j]);
}
Solution 2:
You can remove elements from ArrayList
using ListIterator
,
ListIterator listIterator = List_Of_Array.listIterator();
/* Use void remove() method of ListIterator to remove an element from List.
It removes the last element returned by next or previous methods.
*/
listIterator.next();
//remove element returned by last next method
listIterator.remove();//remove element at 1st position
listIterator.next();
listIterator.next();
listIterator.remove();//remove element at 3rd position
listIterator.next();
listIterator.next();
listIterator.remove();//remove element at 5th position