Alternative of 'for loop' in Java

UPDATED: 15 October 2010
This is something out of the box from regular programming taught in our colleges. I don't know about other countries but in India colleges don't teach programming like this.

Alternative of for loop is called for-each. Lets checkout program...

public class forEach{
 public static void main(String args[]){
  String array[] = {"Vicky","Chirag","Heer"};
  for(String Name : array){
   System.out.println(":"+Name);
  }
 }
}
/** 
  * Output:
  * :Vicky
  * :Chirag
  * :Heer
  */

It will iterate any array till it gets data. Its helps when array generated with dynamic size. Its simple as that. Happy coding...

0 comments :