how to remove items from arraylist in java using for code example

Example 1: java remove from arraylist while iterating

ListIterator iter = books.listIterator();
while(iter.hasNext()){
    if(iter.next().getIsbn().equals(isbn)){
        iter.remove();
    }
}

Example 2: How to remove element from arraylist in java

// java program on ArrayList remove(int index) method example
import java.util.ArrayList;
import java.util.List;
public class ArrayListRemoveIndex
{
   public static void main(String[] args)
   {
      List al = new ArrayList<>();
      al.add(56);
      al.add(28);
      al.add(39);
      al.add(59);
      al.add(82);
      System.out.println("Before using ArrayList.remove(int index) method: " + al);
      // removes element 28
      al.remove(1);
      // element 39 is now moved one position back
      // So element 39 is removed
      al.remove(1);
      System.out.println("After using ArrayList.remove(int index) method: " + al);
   }
}

Tags:

Misc Example