Chapter 3.4.4 – Enums and for each Loops | Introduction to Programming Using Java
3.4.4 Enums and for each Loops
Java 5.0 introduces a new “enhanced” form of the for loop that is designed to be convenient for processing data structures. A data structure is a collection of data items, considered as a unit. For example, a list is a data structure that consists simply of a sequence of items. The enhanced for loop makes it easy to apply the same processing to every element of a list or other data structure.
Data structures are a major topic in computer science, but we won’t encounter them in any serious way until Chapter 7. However, one of the applications of the enhanced for loop is to enum types, and so we consider it briefly here. (Enums were introduced in Subsection 2.3.3.)
The enhanced for loop can be used to perform the same processing on each of the enum constants that are the possible values of an enumerated type. The syntax for doing this is:
If MyEnum is the name of any enumerated type, then MyEnum.values() is a function call that returns a list containing all of the values of the enum. (values() is a static member function in MyEnum and of any other enum.) For this enumerated type, the for loop would have the form:
The intent of this is to execute the hstatementi once for each of the possible values of the MyEnum type. The hvariable-namei is the loop control variable. In the hstatementi, it represents the enumerated type value that is currently being processed. This variable should not be declared before the for loop; it is essentially being declared in the loop itself.
To give a concrete example, suppose that the following enumerated type has been defined to represent the days of the week:
Day.values() represents the list containing the seven constants that make up the enumerated type. The first time through this loop, the value of d would be the first enumerated type value Day.MONDAY, which has ordinal number 0, so the output would be “MONDAY is day number 0”.
The second time through the loop, the value of d would be Day.TUESDAY, and so on through Day.SUNDAY. The body of the loop is executed once for each item in the list Day.values(), with d taking on each of those values in turn. The full output from this loop would be:
Since the intent of the enhanced for loop is to do something “for each” item in a data structure, it is often called a for-each loop. The syntax for this type of loop is unfortunate. It would be better if it were written something like “foreach Day d in Day.values()”, which conveys the meaning much better and is similar to the syntax used in other programming languages for similar types of loops. It’s helpful to think of the colon (:) in the loop as meaning
“in.”
Read More…
Introduction to Programming Using Java – David J. Eck
Chapter 2 – Names and Things | Introduction to Programming Using Java
Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java
Chapter 2.5.1 – Arithmetic Operators | Introduction to Programming Using Java
Chapter 2.5.3 – Relational Operators | Introduction to Programming Using Java
Chapter 2.6.2 – Command Line Environment | Introduction to Programming Using Java
Chapter 2.6.3 – IDEs and Eclipse | Introduction to Programming Using Java
Chapter 3.1.3 – The Basic If Statement | Introduction to Programming Using Java
1 thought on “Chapter 3.4.4 – Enums and for each Loops | Introduction to Programming Using Java”