Monday 30 March 2015

A New way to do the for Loop in Java | advanced for loop in Java | Advanced for Loop in Java ArrayList | advanced for loop in java for list |

This is the new type of for loop which 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);
}

}

Output of the Program

A New way to do the for Loop in Java

Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment