Nov 12

With traditional for loop , we need to know the size of the array for iteration .

But with Enhanced for loop ,array iteration becomes simpler and easier.

Use of  Enhanced for loop :

public class check {

  public static void main(String[] args) {
       int[] numbers = {1,2,3,4};
          System.out.println("Numbers are :");
            for (int num : numbers){
             System.out.println(num);
             }
      }
  }

  • Remember instead of num ,we can use any valid identifier or name.
  • Type of variable used to iterate in Enhanced for loop should match the type of array.
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Anup