java arraylist确保容量不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7688151/
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
java arraylist ensureCapacity not working
提问by kralco626
Either I'm doing this wrong or i'm not understanding how this method works.
要么我做错了,要么我不明白这种方法是如何工作的。
ArrayList<String> a = new ArrayList<String>();
a.ensureCapacity(200);
a.add(190,"test");
System.out.println(a.get(190).toString());
I would have thought that ensureCapacity would let me insert a record with an index up to that value. Is there a different way to do this?
我原以为 ensureCapacity 会让我插入一个索引高达该值的记录。有没有不同的方法来做到这一点?
I get an IndexOutOfBounds error on the third line.
我在第三行收到 IndexOutOfBounds 错误。
回答by Jon Skeet
No, ensureCapacity
doesn't change the logical sizeof an ArrayList
- it changes the capacity, which is the size the list can reach before it next needs to copy values.
不,ensureCapacity
不会更改an的逻辑大小ArrayList
- 它会更改capacity,这是列表在下一次需要复制值之前可以达到的大小。
You need to be very aware of the difference between a logical size (i.e. all the values in the range [0, size)
are accessible, and adding a new element will add it at index size
) and the capacity which is more of an implementation detail really - it's the size of the backing array used for storage.
您需要非常清楚逻辑大小(即范围内的所有值[0, size)
都可以访问,并且添加新元素会将其添加到 index size
)和容量之间的区别,容量实际上更像是一个实现细节 - 这就是大小用于存储的后备阵列。
Calling ensureCapacity
should only ever make any difference in terms of performance (by avoiding excessive copying) - it doesn't affect the logical model of what's in the list, if you see what I mean.
调用ensureCapacity
应该只在性能方面产生任何差异(通过避免过度复制) - 如果您明白我的意思,它不会影响列表中内容的逻辑模型。
EDIT: It sounds like you want a sort of ensureSize()
method, which might look something like this:
编辑:听起来你想要一种ensureSize()
方法,它可能看起来像这样:
public static void ensureSize(ArrayList<?> list, int size) {
// Prevent excessive copying while we're adding
list.ensureCapacity(size);
while (list.size() < size) {
list.add(null);
}
}
回答by trutheality
So as others have mentioned ensureCapacity
isn't for that.
It looks like you want to start out with an ArrayList
of 200 nulls? Then this would be the simplest way to do it:
所以正如其他人所提到的ensureCapacity
,不是为了那个。看起来您想从ArrayList
200 个空值开始?那么这将是最简单的方法:
ArrayList<String> a = new ArrayList<String>(Arrays.asList( new String[200] ));
Then if you want to replaceelement 190 with "test" do:
然后,如果您想用“test”替换元素 190,请执行以下操作:
a.set(190, "test");
This is different from
这不同于
a.add(190, "test");
which will add "test" in index 190 and shift the other 9 elements up, resulting in a list of size 201.
这将在索引 190 中添加“test”并将其他 9 个元素向上移动,从而生成大小为 201 的列表。
If you know you are always going to have 200 elements it might be better to just use an array.
如果你知道你总是会有 200 个元素,那么使用数组可能会更好。
回答by Ryan Stewart
Ensuring capacity isn't adding items to the list. You can only get element 190 or add at element 190 if you've added 191 elements already. "Capacity" is just the number of objects the ArrayList canhold before it needs to resize its internal data structure (an array). If ArrayList had a getCapacity(), then doing this:
确保容量不会将项目添加到列表中。如果您已经添加了 191 个元素,则只能获取元素 190 或添加元素 190。“容量”只是 ArrayList在需要调整其内部数据结构(数组)大小之前可以容纳的对象数量。如果 ArrayList 有一个 getCapacity(),那么这样做:
ArrayList<String> a = new ArrayList<String>();
a.ensureCapacity(200);
System.out.println(a.size());
System.out.println(a.getCapacity());
would print out 0 and some number greater than or equal to 200, respectively
将分别打印出 0 和一些大于或等于 200 的数字
回答by Boann
ArrayList maintains its capacity (the size of the internal array) separately from its size (the number of elements added), and the 'set' method depends on the index already having been assigned to an element. There isn't a way to set the size. If you need this, you can add dummy elements with a loop:
ArrayList 保持其容量(内部数组的大小)与其大小(添加的元素数)分开,并且“set”方法取决于已分配给元素的索引。没有办法设置大小。如果需要,可以使用循环添加虚拟元素:
for (int i = 200; --i >= 0;) a.add(null);
回答by jgon
ensureCapacity
just makes sure that the underlying array's capacity is greater than or equal to the argument. It doesn't change the size of the ArrayList
. It does't make any changes visible through the API, so you won't notice a difference except that it will probably be longer before the ArrayList
resizes it's internal array.
ensureCapacity
只是确保底层数组的容量大于或等于参数。它不会改变ArrayList
. 它不会通过 API 进行任何可见的更改,因此您不会注意到任何差异,只是在ArrayList
调整其内部数组的大小之前可能会更长。
回答by Jan Zyka
Once again JavaDoc to clarify the situation:
再次用JavaDoc澄清情况:
Throws: IndexOutOfBoundsException
- if index is out of range (index < 0 || index > size()).
Note that size()
returns the number of elements currently held by the List.
请注意,size()
返回列表当前持有的元素数。
回答by Andrew Gallasch
Adding 190 null entries to an ArrayList reeks of a misuse of the data structure.
将 190 个空条目添加到 ArrayList 散发着滥用数据结构的恶臭。
Think about using a standard primitive array.
If you require a generics or want more efficient use of space then consider
SparseArray
or even aMap
like aHashMap
may be appropriate for your purposes.
考虑使用标准的原始数组。
如果您需要泛型或想要更有效地利用空间,那么考虑
SparseArray
甚至Map
类似 aHashMap
可能适合您的目的。
回答by sreenath sirimala
public static void fillArrayList(ArrayList<String> arrayList, long size) {
for (int i = 0; i < size + 1; i++) {
arrayList.add(i,"-1");
}
}
public static void main(String[] args) throws Exception {
ArrayList<String> a = new ArrayList<String>(10);
fillArrayList(a, 190);
a.add(190,"test");
System.out.println(a.get(190).toString());
}