For-Each loop

For-Each Loop:

  • Introduce in jdk5.
  • Used to traverse the elements of Collection and arrays.
  • Reduce the probability of programming errors.
  • Make code more readable.


Syntax:


for( (datatype/object) variable:value(array/collection) )
{

}

Example:

ArrayList al=new ArrayList();
al.add(12);al.add("sam");al.add(23);
for(Object obj:al)
{
System.out.println(obj);
}
InCase of Array:
int arr[]={12,23,89};
for(int a:arr){
          System.out.println(a);
}


    ****************************************************************************



Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mobile: 9953584548
Write us at: - Smruti@apextgi.com and pratap@apextgi.com

No comments:

Post a Comment