9. 3. 11. 基于for循环利用集 |
|
You can now use it to iterate over an array or a collection without the index.
Use this syntax to iterate over an array: |
for (componentType variable: arrayName)
|
|
Where arrayName is the reference to the array,
componentType is the component type of the array, and
variable is a variable that references each component of the array. |
import java.util.Arrays;
public class MainClass {
public static void main(String[] arg) {
double[] data = new double[50]; // An array of 50 values of type double
Arrays.fill(data, 1.0); // Fill all elements of data with 1.0
for (double d: data) {
System.out.println(d);
}
}
}
|
|
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0 |