package Devmanuals.com; import java.util.List; import java.util.LinkedList; import java.util.ListIterator; public class ListIteratorRemove { public static void main(String[] args) { LinkedList llist = new LinkedList(); for (int i = 10; i <= 20; i++) llist.add(i); ListIterator litr = llist.listIterator(); System.out.println("Remove the element while travelling in forward direction"); System.out.println("->->->->->->->"); System.out.println("Is list contain more elements ? " + litr.hasNext()); System.out.println("Old list = " + llist); System.out.println("Size of old list = "+llist.size()); while (litr.hasNext()) { Integer i1 = (Integer) litr.next(); if (i1 == 15) litr.remove(); } System.out.println("After removing the last called "); System.out.println("element by iterator in forward direction, then list = " + llist); System.out.println("Size of list after removing element = "+llist.size()); System.out.println(" "); System.out.println("Remove the element while travelling in backward direction"); System.out.println("<-<-<-<-<-<-<-"); System.out.println("Is list contain more elements ? " + litr.hasPrevious()); System.out.println("Old list = " + llist); System.out.println("Size of old list = "+llist.size()); while(litr.hasPrevious()) { Integer i2 = (Integer)litr.previous(); if(i2==18) litr.remove(); } System.out.println("After removing the last called "); System.out.println("element by iterator in backward direction, then list = " + llist); System.out.println("Size of list after removing element = "+llist.size()); } }