
8.3 Aggregate Array Operations 477
8.3.3 Summing the Elements of an Array
To sum the elements of the array, we again use the standard for loop, as
shown in Example 8.5.
1 /* Summing Array Elements
2 Anderson, Franceschi
3 */
4
5 import java.text.NumberFormat;
6
7 public class SummingArrayElements
8 {
9 public static void main( String []args )
10 {
11 double []cellBills = new double [6];
12 cellBills[0] = 45.24;
13 cellBills[1] = 54.67;
14 cellBills[2] = 42.55;
15 cellBills[3] = 44.61;
16 cellBills[4] = 65.29;
17 cellBills[5] = 49.75;
18
19 double totalBills = 0.0; // initialize total
20 for ( int i = 0; i < cellBills.length; i++ )
21 {
22 totalBills += cellBills[i