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[]) {
+"\t** Welcome To **\n"
+"\t** For Each **\n"
+"\t** Demo **\n"

ArrayList<String> list= new <String>ArrayList();
System.out.println("Inserting By Simple For Loop");
for (int i=0;i<10;i++){
System.out.println("Inserting : "+Integer.toString(i+1));
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

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.


Post a Comment