Advance for Loop is a new type of for loop that was added to the Java in 2004. The advantage of this enhanced for loop is that you just iterate linearly through a collection, or linearly through an array, and there is no need to modify the array or collection. The biggest advantages of this advanced for loop is that you no need to deal with the index numbers of array or array list and that's why this loop is used only in specific cases.
Source Code
package temp; import java.util.ArrayList; /** * @author Uzair */ public class ForEach { public static void main(String args[]) { System.out.println("\n\n\n" +"\t*******************************************************\n" +"\t** Welcome To **\n" +"\t** For Each **\n" +"\t** Demo **\n" +"\t*******************************************************\n"); System.out.println("\t\t\t"); ArrayList<String> list= new <String>ArrayList(); System.out.println("Inserting By Simple For Loop"); for (int i=0;i<10;i++){ list.add(Integer.toString(i+1)); System.out.println("Inserting : "+Integer.toString(i+1)); } System.out.println("**********************************\n"); System.out.println("Retrieving By Simple For-Each Loop"); for(Object s : list) System.out.println("Retrieving : "+s); } }
0 comments:
Post a Comment