Pages

Monday 12 March 2012

for Each loop

Java for each loop is a new feature from 1.5 version.It is used to iterate over arrays.
for example

String[] flowers={"rose","lilly","daisy"};
System.out.println("The flowers are");
for(String flower : flowers){
System.out.println(flower);
}

It will print

The flowers are
rose
lilly
daisy

so from the array flowers it fetches all the individual elements and assigns them to the new local variable flower.
It works for all types of arrays.We can use it for multi dimensional arrays also.

No comments:

Post a Comment