ArrayListIteratorRemoveExample.java
import java.util.ArrayList; import java.util.Iterator; /* * Example of Iterator remove() method */ public class ArrayListIteratorRemoveExample { public static void main( String[] args ) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Ram"); arrayList.add("Dave"); arrayList.add("Peter"); arrayList.add("Julia"); arrayList.add("Akram"); System.out.println("arrayList : " + arrayList + "\n"); Iterator<String> iterator = arrayList.iterator(); while( iterator.hasNext() ) { iterator.next(); /* * Removes from the list the last element that was returned by * next() */ iterator.remove(); } System.out.println("arrayList : " + arrayList + "\n"); } }
arrayList : [Ram, Dave, Peter, Julia, Akram] arrayList : []
https://sites.google.com/site/javaee4321/java-collections/ArrayListDemoItrRemove.zip?attredirects=0&d=1
See also:
No comments:
Post a Comment