For Each loop in java 1.5

By Vinay | April 29, 2009 | 1,262 views
Category Java

  Share


About author  I am Java/oracle professional.Working on Java/J2EE technologies and i.e Oracle ADF,Spring,hibernate,J2ee,PL/sql,Apps for 2+ years.I am passionate about learning new technologies.I am sharing my knowledge. Give your views and suggestion. http://www.linkedin.com/in/vinaykumar2 Read more from this author


The for-each loop, or enhanced for loop, is new feature in JDK 1.5 to remove clutter and possible errors, in iterating over collections.This will not provide any new functionality .It is just making our code more easy.It allow to create one variable which will iterate over array or collection.By this we can do nested iteration.

JDK 5.0 provides a special kind of for loop for access through all the elements of it.

Syntax 

For(vaiable : collection){ Statements; }
Example:

  int[] a={1,2,3,4,5,6,7,8,9,0};
Using for Each loop :-

  for(int i : a){
    System.out.println(i);</em>
  }  

using Simple for loop :-

for(Iterator itr = a.iterator(); i.hasNext(); ) {
  String item = i.next();
  System.out.println(item);one more example will clear you more on this.

Using Foreach with Arrays  :-

String[] letters = { "v", "i", "n" , "a" , "y" };
for (String letter: letters)
   System.out.println(letter.charAt(0));
  • Delicious
  • Yahoo Buzz
  • Digg
  • DZone
  • Facebook
  • LinkedIn
  • Twitter
  • Share/Bookmark
Read more post on For Each loop for-each loop in java 1.5 Oracle ADF 

  Share

Leave a Comment

Name:

E-Mail :

Website :

Comments :