- The for loop
- The while loop or do..while
- The while+iterator loop
import java.util.ArrayList; import java.util.Iterator; public class main{ public static void main(String[] args) { ArrayListCompilation and execution:arraylist = new ArrayList (); arraylist .add(1); arraylist .add(2); arraylist .add(3); System.out.println("for loop"); for(int i = 0 ; i < arraylist .size(); i++) System.out.println(arraylist .get(i)); System.out.println("Advanced for loop"); for(Integer n : arraylist) System.out.println(n); System.out.println("while+iterator loop"); int i = 0; while(i iterator = arraylist.iterator(); while(iterator.hasNext()) System.out.println(iterator.next()); } }
for loop 1 2 3 Advanced for loop 1 2 3 while+iterator loop 1 2 3
How to browse an ArrayList using the Enumeration interface
import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; public class main{ public static void main(String[] args) { ArrayListalist = new ArrayList (); alist.add("a"); alist.add("b"); alist.add("c"); // get Enumeratioin object Enumeration enumeration = Collections.enumeration(alist); // iterate while(enumeration.hasMoreElements()) System.out.println(enumeration.nextElement()); } }
After compilation and execution of this code:
a b c
No comments:
Post a Comment