如何调整ArrayList的大小
我来自 C++ 背景,我想要一个矩阵
ArrayList<arrayList<E>> javamatrix
在 C++ 中我会做
std::vector<std::vector<T> > cppmatrix;
std::vector<T>vcol(cols);
cppmatrix.resize(rows,vcol);
我似乎找不到 ArrayLists
的内置 resize()
函数> 对于这个任务,我应该使用另一个集合吗?除了使用 javamatrix.add() 的 for 循环之外,没有其他办法可以做到这一点吗?
PS 我希望它在构造函数中使用其大小进行初始化,因为在编辑元素或添加或删除之前可能会查询该大小。
I come from a C++ background and I want to have a matrix of
ArrayList<arrayList<E>> javamatrix
In C++ I would just do
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()
?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有自动构造和添加元素的
resize
等效项。你必须自己做这件事。但是,ensureCapacity< /code>
相当于向量的
reserve
。它将确保您有空间,但不会改变实际大小。There is no
resize
equivalent that automatically constructs and adds elements. You must do this yourself. However,ensureCapacity
is equivalent to vector'sreserve
. It will ensure you have room, but not change the actual size.您不需要调整数组列表的大小。您最初传入的大小只是其起始大小。如果您尝试添加超出其当前大小的项目,它将自动调整大小。
来自文档:
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:
我知道这个问题已经很老了,但是这个链接可能会有所帮助 java arraylist EnsureCapacity not working ,代码添加“Null”值以调整当前大小。
您可以使用ensureSize,而不是纯粹使用ensureCapacity
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.
Instead of using purely ensureCapacity you can have ensureSize
大多数情况下,不需要“resize()”操作,因为 (a) ArrayList 在添加元素时自动调整大小,并且 (b) 不清楚您将在 ArrayList<> 中存储什么值,例如“null”不太适合有用。例如,在您的情况下,您可能无论如何都需要一个循环来创建 MatrixCell 对象。
对于那些想知道如何调整 ArrayList 大小以使其变小的读者来说,我很困惑为什么 ArrayList 的设计没有使用“resize()”方法。也许是因为新手程序员可能会看到该方法,然后没有意识到 ArrayList<> 的作用。自动调整大小。
在 Java 中,这个习惯用法可以减少 ArrayList<> 的大小:
它之所以有效,是因为“subList”返回一个由原始 ArrayList<> 支持的列表,因此“clear()”对原始“ArrayList<>”进行操作。
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.
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.
In Java this idiom works to reduce the size of an ArrayList<>:
It works because the 'subList' returns a List backed by the original ArrayList<>, so therefore the 'clear()' operates on the original 'ArrayList<>'.