Enhanced for loop

The new version eliminates the loop counter.

The new syntax is:


for (type variable_name:array){
       
}

The type must be compatible with the array type.


public class Main {
  public static void main(String args[]) {
    String[] arr = new String[]{"java2s.com","a","b","c"};
    for(String s:arr){
      System.out.println(s);
    }
  }
}

The output:


java2s.com
a
b
c
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.