Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
7.7 Passing Arrays to Methods 259 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // Fig. 7.12: EnhancedForTest.java // Using the enhanced for statement to total integers in an array. public class EnhancedForTest { public static void main( String[] args ) { int[] array = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 }; int total = 0; // add each element's value to total for ( int number : array ) total += number; System.out.printf( "Total of array elements: %d\n", total ); } // end main } // end class EnhancedForTest Total of array elements: 849 Fig. 7.12 | Using the enhanced for statement to total integers in an array. The enhanced for statement simplifies the code for iterating through an array. Note,