8) Arrays & ArrayLists Lesson

How to Use Java For-Each with Collections

5 min to complete · By Ryan Desmond

In the introduction to for loops, there was a brief introduction to the for-each loop. Now that you're working with Arrays and ArrayLists, both of which are subclasses of the Collections class in Java, you can take advantage of Java's for-each loop.

When to Use a For-Each Loop?

Like a traditional for loop, for-each loops are ideally suited for iterating through Arrays and ArrayList. However, a for-each Loop does not have an index for us to use, but it will visit every element in a collection in sequential order.

So, if you need to access specific elements in an array at a specific index, you'll want to use a traditional for loop. That said, if you need/want to visit every element in an array (without specifying any index), you can use a for-each loop. 

For-Each and Collection Examples

Below are two examples of using a for-each loop with Collections.

1D Array and For-Each

In this example, the for-each loop is applied to a single-dimensional array.

class Main {
  public static void main(String[] args){
    int[] vals = new int[5];
    //  use a traditional for loop to populate the array
    // specific indices needed
    for (int i = 0; i < vals.length; i++){
      vals[i] = i * 2;
    }
    // currently, the "vals" array looks like [0, 2, 4, 6, 8]
    // now you'll use a _for-each_ Loop to iterate 
    // through the array and print each value
    for(int val : vals){
      // here, "val" is the actual value in "vals" 
      // the for-each loop will iterate through the entire array 
      // on each loop the value of "val" will change
      System.out.println(val);
    }
  }
}

2D Array and For-Each

The for-each loop is also great at visiting every element in a two-dimensional array. In the example below, there is a nested set of traditional for loops to populate a 2D array. Then, there's a nested set of for-each loops to print out each value in the array.

class Main {
  public static void main(String[] args){
    int[][] nums = new int[5][10];
    for (int i = 0; i < nums.length; i++){
      for (int j = 0; j < nums[i].length; j++){
        nums[i][j] = (i * j) * 7 / 3;
      }
    }
    for(int[] outer : nums){
      for(int val : outer){
        System.out.print(val + " -> ");
      }
      System.out.println();
    }
  }
}

As you can see, the for-each loop has a shorter syntax and makes the code more readable.

Experiment with Java's For-Each Loop

In the code editor below, please demonstrate how to:

  • Declare an Array or ArrayList
  • Populate the Array or ArrayList
  • Use a for-each loop to print each element
class Main {
  public static void main(String[] args) {
    // declare an array or arraylist below

    // populate that array or arraylist

    // use a For Each Loop to print each element 

    // keep your code above this line
  }
}

Summary: How to Use Java Collections and For Each

  • A for-each loop does not provide an index
  • A for-each loop will iterate over every element
  • A for-each loop is excellent for iterating a collection when you know that every element must be visited
  • A for-each loop can work on multidimensional Arrays