Java for each loop through List

Introduction

We can use the for-each loop to iterate all elements from a List

for (int v : collectionObject){
   //your code   
}

Full source


// Use the for-each for loop to cycle through a collection. 
import java.util.ArrayList;

public class Main {
  public static void main(String args[]) {
    // Create an array list for integers.
    ArrayList<Integer> vals = new ArrayList<Integer>();

    // Add values to the array list.
    vals.add(1);/*from  w  ww . j av a  2  s .co  m*/
    vals.add(2);
    vals.add(3);
    vals.add(4);
    vals.add(5);

    // Use for loop to display the values.
    System.out.print("Original contents of vals: ");
    for (int v : vals)
      System.out.print(v + " ");
    System.out.println();

    // Now, sum the values by using a for loop.
    int sum = 0;
    for (int v : vals)
      sum += v;

    System.out.println("Sum of values: " + sum);
  }
}



PreviousNext

Related