Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 204

The for-each Loop

Оглавление

The for-each loop is a specialized structure designed to iterate over arrays and various Collections Framework classes, as presented in Figure 3.8.


FIGURE 3.8 The structure of an enhanced for-each loop

The for-each loop declaration is composed of an initialization section and an object to be iterated over. The right side of the for-each loop must be one of the following:

 A built-in Java array

 An object whose type implements java.lang.Iterable

We cover what implements means in Chapter 7, but for now you just need to know that the right side must be an array or collection of items, such as a List or a Set. For the exam, you should know that this does not include all of the Collections Framework classes or interfaces, but only those that implement or extend that Collection interface. For example, Map is not supported in a for-each loop, although Map does include methods that return Collection instances.

The left side of the for-each loop must include a declaration for an instance of a variable whose type is compatible with the type of the array or collection on the right side of the statement. On each iteration of the loop, the named variable on the left side of the statement is assigned a new value from the array or collection on the right side of the statement.

Compare these two methods that both print the values of an array, one using a traditional for loop and the other using a for-each loop:

public void printNames(String[] names) { for(int counter=0; counter<names.length; counter++) System.out.println(names[counter]); } public void printNames(String[] names) { for(var name : names) System.out.println(name); }

The for-each loop is a lot shorter, isn't it? We no longer have a counter loop variable that we need to create, increment, and monitor. Like using a for loop in place of a while loop, for-each loops are meant to reduce boilerplate code, making code easier to read/write, and freeing you to focus on the parts of your code that really matter.

We can also use a for-each loop on a List, since it implements Iterable.

public void printNames(List<String> names) { for(var name : names) System.out.println(name); }

We cover generics in detail in Chapter 9, “Collections and Generics.” For this chapter, you just need to know that on each iteration, a for-each loop assigns a variable with the same type as the generic argument. In this case, name is of type String.

So far, so good. What about the following examples?

String birds = "Jay"; for(String bird : birds) // DOES NOT COMPILE System.out.print(bird + " "); String[] sloths = new String[3]; for(int sloth : sloths) // DOES NOT COMPILE System.out.print(sloth + " ");

The first for-each loop does not compile because String cannot be used on the right side of the statement. While a String may represent a list of characters, it has to actually be an array or implement Iterable. The second example does not compile because the loop type on the left side of the statement is int and doesn't match the expected type of String.

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Подняться наверх