Java 如何调整 ArrayList 的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4170508/
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
How to resize ArrayList
提问by Ismail Marmoush
I come from a C++ background and I want to have a matrix of
我来自 C++ 背景,我想要一个矩阵
ArrayList<arrayList<E>> javamatrix
In C++ I would just do
在 C++ 中,我会做
std::vector<std::vector<T> > cppmatrix;
std::vector<T>vcol(cols);
cppmatrix.resize(rows,vcol);
I can't seem to find a built-in resize()
function for ArrayLists
for this task, so should I use another collection? Is no way to do this except using for loops with javamatrix.add()
?
我似乎无法为此任务找到内置resize()
函数ArrayLists
,所以我应该使用另一个集合吗?除了使用 for 循环之外没有办法做到这一点javamatrix.add()
吗?
P.S I want it to be initialized in the constructor with its size as that size might be queried before I edit elements or add or remove.
PS我希望它在构造函数中使用其大小进行初始化,因为在我编辑元素或添加或删除之前可能会查询该大小。
采纳答案by Matthew Flaschen
There is no resize
equivalent that automatically constructs and adds elements. You must do this yourself. However, ensureCapacity
is equivalent to vector's reserve
. It will ensure you have room, but not change the actual size.
没有resize
自动构造和添加元素的等价物。你必须自己做这件事。但是,ensureCapacity
相当于向量的reserve
。它将确保您有空间,但不会更改实际大小。
回答by Dave McClelland
You shouldn't need to resize arraylists. The size you initially pass in is just its starting size. If you attempt to add items beyond its current size, it will automatically resize.
您不需要调整数组列表的大小。您最初传入的大小只是它的起始大小。如果您尝试添加超出其当前大小的项目,它将自动调整大小。
From the documentation:
从文档:
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
每个 ArrayList 实例都有一个容量。容量是用于存储列表中元素的数组的大小。它始终至少与列表大小一样大。随着元素被添加到 ArrayList,它的容量会自动增长。除了添加元素具有恒定的摊销时间成本之外,没有指定增长政策的细节。
回答by Tim Cooper
Mostly, a 'resize()' operation is not needed because (a) ArrayList's auto-resize as you add elements, and (b) it's unclear what values you would store in the ArrayList<>, e.g. 'null' is not very useful. E.g. in your case you'd probably need a loop anyway to create MatrixCell objects.
大多数情况下,不需要“resize()”操作,因为 (a) ArrayList 在添加元素时会自动调整大小,以及 (b) 不清楚您将在 ArrayList<> 中存储哪些值,例如“null”不是很有用. 例如,在您的情况下,您可能需要一个循环来创建 MatrixCell 对象。
For those readers who want to know how to resize an ArrayList to make it smaller, it mystifies me why ArrayList was designed without a 'resize()' method. Perhaps it's because novice programmers are likely to see that method and then not realise that ArrayList<> auto-resizes.
对于那些想知道如何调整 ArrayList 的大小以使其更小的读者,这让我感到困惑,为什么 ArrayList 的设计没有“resize()”方法。也许是因为新手程序员可能会看到该方法,然后没有意识到 ArrayList<> 会自动调整大小。
In Java this idiom works to reducethe size of an ArrayList<>:
在 Java 中,这个习惯用法可以减小ArrayList<> 的大小:
list.subList(n,list.size()).clear();
It works because the 'subList' returns a List backed by the original ArrayList<>, so therefore the 'clear()' operates on the original 'ArrayList<>'.
它有效是因为“subList”返回一个由原始 ArrayList<> 支持的 List,因此“clear()”对原始“ArrayList<>”进行操作。
回答by neferpitou
I know this question is very old already but this link may help java arraylist ensureCapacity not working, The code adds "Null" value in order to adjust the current size.
我知道这个问题已经很老了,但是这个链接可能会帮助java arraylist ensureCapacity 不起作用,代码添加了“Null”值以调整当前大小。
Instead of using purely ensureCapacity you can have ensureSize
您可以使用 ensureSize 而不是纯粹使用 ensureCapacity
public static void ensureSize(ArrayList<?> list, int size) {
list.ensureCapacity(size);
while (list.size() < size) {
list.add(null);
}
}