- Here is how to
sum all the
elements in
the array myList
using a
for-each loop:
public static void main(String[] args)
{
double[] myList = { 1, 2, 3, 4, 5, 6};
double sum;
// Compute sum = sum all elements in array myList
sum = 0;
for ( double e: myList )
sum = sum + e;
System.out.println("sum = " + sum);
}
|
- Notes:
- The for-each loop
do not
use an
index to
access the
array elements
- You
cannot
update
array elements with
a for-each loop
|
|