Java的ArrayList中增加电流的大小外项目 [英] Java ArrayList add item outside current size
问题描述
不知道是否有一个项目在比它更大的位置添加到Java的ArrayList的一个有效的方法的当前大小:
Wondering if there is an efficient way to add an item to Java's ArrayList in a position that is bigger than it's current size :
情景:
ArrayList<Item> items = new ArrayList<Item>;
.. let's say I add three elements
现在,我想在10位(留下的物品从3到10为空)添加一个项目
Now I would like to add an item at position 10 (leaving items from 3 to 10 to null)
items.add(10,newItem); // item.size() == 3
有没有一种有效的方式调整/填充一个ArrayList用null?
Is there an efficient way resizing/filling an ArrayList with nulls ?
Java的实施,使得大小字段私营: - (..
Java's implementation makes size field private :-(..
推荐答案
恕我直言,你能做的最好的事情是 items.addAll(Collections.nCopies(6,空))
和希望,即ArrayList中实现了一些行为,在内部上这个了。
imho the best thing you can do is items.addAll(Collections.nCopies(6, null))
and hope, that ArrayList implements some behaviour to internally fasten this up
这篇关于Java的ArrayList中增加电流的大小外项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!