Specifications

Sun Services
Java™ Programming Language
Module 9, slide 37 of 37
Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F
Enhanced for Loop
Using enhanced for loop in arrays:
public int sum(int[] array){
int result = 0;
for ( int element : array ) {
result += element;
}
return result;
}
Using enhanced for loop in nested loops:
List<Subject> subjects=...;
List<Teacher> teachers=...;
List<Course> courseList = new ArrayList<Course>();
for ( Subject subj : subjects ) {
for ( Teacher tchr : teachers ) {
courseList.add(new Course(subj, tchr));
}
}