java 将前 100 个整数添加到 ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28550489/
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
Adding the first 100 integers to ArrayList
提问by Aaron Gabriel
I am writing a program to display the first 100 integers ArrayList. It complies but nothing runs. Can anyone tell me where I am going wrong. I have tried to add the first 100 integers and then display them, but once again I get a blank page. Here is what I have done.
我正在编写一个程序来显示前 100 个整数 ArrayList。它符合但没有任何运行。谁能告诉我我哪里出错了。我试图添加前 100 个整数然后显示它们,但我再次得到一个空白页。这是我所做的。
ArrayList<Integer> numbers = new ArrayList<Integer>(100);
for (Integer i : numbers)
{
numbers.add(i);
System.out.println(numbers.get(i));
}
回答by M Anouti
The 100
argument in the ArrayList
constructor specifies the initial capacityof the list. It does not mean that the actual number of the elements is 100
. See the documentation of the ArrayList
constructor:
将100
在参数ArrayList
构造函数指定的初始容量的列表。这并不意味着元素的实际数量是100
。请参阅ArrayList
构造函数的文档:
Constructs an empty listwith the specified initial capacity
构造一个具有指定初始容量的空列表
Since your list is empty, the for (Integer i : numbers)
loop will therefore execute zero times. You need to loop as follows:
由于您的列表为空,for (Integer i : numbers)
因此循环将执行零次。你需要循环如下:
ArrayList<Integer> numbers = new ArrayList<Integer>(100);
for (int i = 1; i <= 100; i++)
{
numbers.add(i);
System.out.println(numbers.get(i - 1));
}
ArrayList.get(i)
gets the element at index i
. Since the index starts at zero, the last statement in the loop body should be numbers.get(i - 1)
.
ArrayList.get(i)
获取 index 处的元素i
。由于索引从零开始,循环体中的最后一条语句应该是numbers.get(i - 1)
.
If the numbers should start at zero (0
to 99
), then the loop would be as follows:
如果数字应该从零开始 ( 0
to 99
),那么循环将如下所示:
for (int i = 0; i < 100; i++)
{
numbers.add(i);
System.out.println(numbers.get(i));
}
回答by Makoto
for (Integer i : numbers)
is the enhanced-for statement. It effectively iterates over each entry contained in numbers
, which is referenced for that iteration by i
, and allows you to do things with respect to i
.
for (Integer i : numbers)
是增强的 for 语句。它有效地迭代包含在 中的每个条目numbers
,该条目被该迭代引用i
,并允许您执行与 相关的操作i
。
Since numbers
is empty, the loop won't run.
由于numbers
为空,循环不会运行。
Further, new ArrayList<>(100)
will set the initial capacity of the array list to 100; this is useful if you know that you don't want to incur the cost of the array list increasing its size every time it hits the limit. It does notfill any values into the array list.
进一步,new ArrayList<>(100)
将数组列表的初始容量设置为100;如果您知道不想在每次达到限制时增加数组列表的大小,这将非常有用。它不会将任何值填充到数组列表中。
All you really need to do is change the enhanced-for to a normal for
instead:
您真正需要做的就是将enhanced-for 改为普通for
:
for(int i = 0; i < 100; i++) {
numbers.add(i);
}
回答by David.Jones
You are looping through an empty list. You need to change you code to add values to this ArrayList, then print them out:
您正在遍历一个空列表。您需要更改代码以向此 ArrayList 添加值,然后将它们打印出来:
int initialSize = 100;
ArrayList<Integer> numbers = new ArrayList<Integer>(initialSize);
for (int i = 0; i < initialSize; i++)
{
numbers.add(i);
System.out.println(numbers.get(i));
}