Loops on Collections: while loop, iterator, and for each  

by ne on 2021-09-29 under Asp.net

	import java.util.ArrayList;
	import java.util.Iterator;

	public class Main {
	  public static void main(String[] args) {
      ArrayList list = new ArrayList(); 
 
    	 list.add("Monday"); 
    	 list.add("Tuesdag"); 
    	 list.add("Wednesday"); 
    	 list.add("Thursday"); 
    	 list.add("Friday"); 
    	 list.add("Saturday"); 
    	 list.add("Sunday"); 
 
	 Iterator iterator = null; 
 
	 iterator = list.iterator(); 
	 while (iterator.hasNext()) { 
	     String element = iterator.next(); 
	     System.out.println(element); 
	 } 
 
	 for (iterator = list.iterator(); iterator.hasNext();) { 
	     String element = iterator.next(); 
	     System.out.println(element); 
	 } 
 
	 for (String element : list) { 
	     System.out.println(element); 
	 } 
 } 
	} 
	/*
    Output of the code above : 
    	Monday 
    	Tuesdag 
    	Wednesday 
    	Thursday 
    	Friday 
    	Saturday 
    	Sunday 
    	Monday 
    	Tuesdag 
    	Wednesday 
    	Thursday 
    	Friday 
    	Saturday 
    	Sunday 
    	Monday 
    	Tuesdag 
    	Wednesday 
    	Thursday 
    	Friday 
    	Saturday 
    	Sunday 
	*/ 
 

if this code is help full for you than please comment on it

Thanks..:)