You can loop through the array elements with the for
loop, and use the length
property to specify how many times the loop should run.
The following example outputs all elements in the cars array:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
There is also a "for-each" loop, which is used exclusively to loop through elements in arrays:
for (type variable : arrayname) {
...
}
The following example outputs all elements in the cars array, using a "for-each" loop:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
System.out.println(i);
}
The example above can be read like this: for each String
element (called i - as in index) in cars, print out the value of i.
If you compare the for
loop and for-each loop, you will see that the for-each method is easier to write, it does not require a counter (using the length property), and it is more readable.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!