Use a for-each style for loop : For Each Loop « Statement Control « Java Tutorial






public class MainClass { 
  public static void main(String args[]) { 
    int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
    int sum = 0; 

    // use for-each style for to display and sum the values
    for(int x : nums) { 
      System.out.println("Value is: " + x);
      sum += x; 
    } 

    System.out.println("Summation: " + sum);
  } 
}
Value is: 1
Value is: 2
Value is: 3
Value is: 4
Value is: 5
Value is: 6
Value is: 7
Value is: 8
Value is: 9
Value is: 10
Summation: 55








4.7.For Each Loop
4.7.1.The For-Each Version of the for Loop
4.7.2.The for-each loop is essentially read-only
4.7.3.The for each loop for an enum data type
4.7.4.Using the For-Each Loop with Collections: ArrayList
4.7.5.Use a for-each style for loop
4.7.6.Using 'for each' to loop through array
4.7.7.Iterating over Multidimensional Arrays: Use for-each style for on a two-dimensional array
4.7.8.Using break with a for-each-style for