2014年5月7日星期三

ArrayList 用法

When you call new ArrayList<Integer>(10), you are setting the list's initial capacity, not its size. In other words, when constructed in this manner, the array list starts its life empty.
One way to add ten elements to the array list is by using a loop:
for (int i = 0; i < 10; i++) {
  arr.add(0);
}
Having done this, you can now modify elements at indices 0..9.

So you can only add elements first after you create ArrayList. You can't use Set to change value because it is empty at beginning.

没有评论:

发表评论