Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Java
Expertise: Beginner
Feb 4, 2004



Building the Right Environment to Support AI, Machine Learning and Deep Learning

The Enhanced For Loop

In the field of information technology, what counts more is the early adaptation of the new features that come with every new version of software. Coming up with the JDK 1.5 (tiger) release of Java is a new feature of the enhanced for loop. Let see how it works!

In the enhanced for loop, the array index is not necessary for the retrieval of an array element:

int arr[5] = {2,4,8,16,32};


for (int i=0; i<5; i++)

                System.out.println("Output: " + arr[i]);
Now, you can save a few keystrokes and retrieve an array element thusly:


for(int a:arr)                      //  check syntax, it's colon not semi-colon 

                System.out.println("Output: "+ a);
The enhanced for loop also works with collection frameworks. So instead of using Iterators (which are unually used to get elements out of a collection), you can do something like this:

Vector vec = new Vector();

                vec.addElement(new Integer(2));

                vec.addElement(new Integer(4));


for(Object obj:vec)

                                                System.out.println("Output: "+(Integer)obj);
M.Fahim Zahid
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date