1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
/* Iterate through a Collection using Java Iterator Example This Java Example shows how to iterate through a Collection using Java Iterator. */ import java.util.Iterator; import java.util.ArrayList; public class JavaIteratorExample { public static void main(String[] args) { //create an ArrayList object ArrayList aList = new ArrayList(); //populate ArrayList object aList.add("1"); aList.add("2"); aList.add("3"); aList.add("4"); aList.add("5"); /* Get Iterator object by invoking iterator method of collection. Iterator provides hasNext() method which returns true if has more elements. next() method returns the element in iteration. */ Iterator itr = aList.iterator(); //iterate through the ArrayList values using Iterator's hasNext and next methods while(itr.hasNext()) System.out.println(itr.next()); /* Please note that next method may throw a java.util.NoSuchElementException if iteration has no more elements. */ } } /* Output would be 1 2 3 4 5 */ |
Cool very good thanks
thanks