Java 遍历可变长度数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2331509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterating through a variable length array
提问by Ankur
How do I iterate over a Java array of variable length.
如何遍历可变长度的 Java 数组。
I guess I would setup a while loop, but how would I detect that I have reached the end of the array.
我想我会设置一个 while 循环,但是我如何检测到我已经到达数组的末尾。
I guess I want something like this [just need to figure out how to represent myArray.notEndofArray()]
我想我想要这样的东西 [只需要弄清楚如何表示 myArray.notEndofArray()]
index = 0;
while(myArray.notEndofArray()){
system.out.println(myArray(index));
index++;
}
采纳答案by TofuBeer
for(int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
or
或者
for(String value : array)
{
System.out.println(value);
}
The second version is a "for-each" loop and it works with arrays and Collections. Most loops can be done with the for-each loop because you probably don't care about the actual index. If you do care about the actual index us the first version.
第二个版本是“for-each”循环,它适用于数组和集合。大多数循环都可以使用 for-each 循环来完成,因为您可能并不关心实际的索引。如果您确实关心我们的第一个版本的实际索引。
Just for completeness you can do the while loop this way:
为了完整起见,您可以通过以下方式执行 while 循环:
int index = 0;
while(index < myArray.length)
{
final String value;
value = myArray[index];
System.out.println(value);
index++;
}
But you should use a for loop instead of a while loop when you know the size (and even with a variable length array you know the size... it is just different each time).
但是当您知道大小时,您应该使用 for 循环而不是 while 循环(即使使用可变长度数组,您也知道大小......每次都不同)。
回答by Edward Q. Bridges
Arrays have an implicit member variable holding the length:
数组有一个包含长度的隐式成员变量:
for(int i=0; i<myArray.length; i++) {
System.out.println(myArray[i]);
}
Alternatively if using >=java5, use a for each loop:
或者,如果使用 >=java5,则为每个循环使用一个:
for(Object o : myArray) {
System.out.println(o);
}
回答by Kevin Bourrillion
You've specifically mentioned a "variable-length array" in your question, so neither of the existing two answers (as I write this) are quite right.
您在问题中特别提到了“可变长度数组”,因此现有的两个答案(如我所写)都不完全正确。
Java doesn't have any concept of a "variable-length array", but it does have Collections, which serve in this capacity. Any collection (technically any "Iterable", a supertype of Collections) can be looped over as simply as this:
Java 没有任何“可变长度数组”的概念,但它确实有集合,可以提供这种功能。任何集合(技术上任何“可迭代”,集合的超类型)都可以像这样简单地循环:
Collection<Thing> things = ...;
for (Thing t : things) {
System.out.println(t);
}
EDIT:it's possible I misunderstood what he meant by 'variable-length'. He might have just meant it's a fixed length but not every instance is the same fixed length. In which case the existing answers would be fine. I'm not sure what was meant.
编辑:我可能误解了他所说的“可变长度”是什么意思。他可能只是意味着它是固定长度,但并非每个实例都是相同的固定长度。在这种情况下,现有的答案就可以了。我不确定是什么意思。
回答by cjonas
here is an example, where the length of the array is changed during execution of the loop
这是一个示例,其中在循环执行期间更改数组的长度
import java.util.ArrayList;
public class VariableArrayLengthLoop {
public static void main(String[] args) {
//create new ArrayList
ArrayList<String> aListFruits = new ArrayList<String>();
//add objects to ArrayList
aListFruits.add("Apple");
aListFruits.add("Banana");
aListFruits.add("Orange");
aListFruits.add("Strawberry");
//iterate ArrayList using for loop
for(int i = 0; i < aListFruits.size(); i++){
System.out.println( aListFruits.get(i) + " i = "+i );
if ( i == 2 ) {
aListFruits.add("Pineapple");
System.out.println( "added now a Fruit to the List ");
}
}
}
}