import java.io.*; import java.util.*; public class Example1 { public static void main(String[] args) { Listnums = new ArrayList (); nums.add(10); nums.add(20); nums.add(30); Iterator itr = nums.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } }
import java.io.*; import java.util.*; public class Example2 { public static void main(String[] args) { ListIn this example, we create a List of strings and use the iterator to remove the element "Alice" from the list. As mentioned earlier, the java.io package library contains the Iterator interface.names = new ArrayList (); names.add("John"); names.add("Alice"); names.add("Bob"); Iterator itr = names.iterator(); while(itr.hasNext()){ String name = itr.next(); if(name.equals("Alice")){ itr.remove(); } } System.out.println(names); } }