0
public static double average( double... numbers ) { 
    double total = 0.0; 
    [B][I][COLOR="Red"]for (double d : numbers)[/[/COLOR]I][/B] { 
        total += d; 
    } 
    return total / numbers.length; 
} 

can any one explain the line bold
if posseble d program too...

Edited by mike_2000_17: Fixed formatting

4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by tong1
0

"Enhanced for loop" - new in Java 1.5, and very good.
Read it as "for each double d in numbers"
It copies each element of numbers in turn to a new double, and executes the loop once for each.
You can use this for arrays, lists, all kinds of collections, eg:

ArrayList<String> someStrings = // create and populate the arraylist of Strings
for (String s : someStrings) {
   // this is executed once for each String in someStrings.
   // the value of each String is in variable s
}
0

for (double d : numbers) {
total += d;
}

is equivalent to:

for (int i=0; i<numbers.length;i++){
double d = numbers[i];
total += d;
}

Edited by tong1: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.