AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-new-features: What is advance for loop in java ?

How to use advance for loop in java. Please give me brief introduction of advance for loop in java
with sample code.

corejava x 353
new-features x 5
Posted On : 2013-10-30 10:32:45.0
profile Garima Gupta - anyforum.in Garima Gupta
596129560202
up-rate
5
down-rate

Answers


The for-each(Advance for loop) loop introduced in Java5. It is mainly used to traverse array or
collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and
makes the code more readable. we don?t need to traverse elements manually.

===========================Syntax of for-each loop:=============================
for(data_type variable : array | collection){}


==============================Sample code:===================================

++++++++++++++++++++++Example to traverse Array Elements:+++++++++++++++++++++

class ForEachArraay{
public static void main(String args[]){
int arr[]={11,14,13,15};

for(int i:arr){
System.out.println(i);
}

}
}

Output:
11
14
13
15

++++++++++++++++++++++Example to traverse List Elements:+++++++++++++++++++++

import java.util.*;
class ForEachArrayList{
public static void main(String args[]){
ArrayList list=new ArrayList();
list.add("Garima");
list.add("anyforum.in");
list.add("Sailesh");

for(String s:list){
System.out.println(s);
}

}
}

Output:
Garima
anyforum.in
Sailesh

Posted On : 2013-10-30 11:07:00
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188250043
Reply This Thread
up-rate
5
down-rate

Advance for loop uses as nested loop in Array..

Posted On : 2014-07-03 09:49:29
Satisfied : 0 Yes  1 No
profile Mukesh chandra - anyforum.in Mukesh chandra
020
Reply This Thread
up-rate
2
down-rate
Comments
Please make your point clear. It´s not sufficient to explain
profile Garima Gupta - anyforum.in Garima Gupta
596  1295  60202
Posted On :2014-07-03 17:59:18.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in