如何调整ArrayList的大小

发布于 2024-10-01 17:08:31 字数 461 浏览 1 评论 0原文

我来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神魇的王 2024-10-08 17:08:31

没有自动构造和添加元素的 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's reserve. It will ensure you have room, but not change the actual size.

南城追梦 2024-10-08 17:08:31

您不需要调整数组列表的大小。您最初传入的大小只是其起始大小。如果您尝试添加超出其当前大小的项目,它将自动调整大小。

来自文档

每个 ArrayList 实例都有一个容量。容量是用于存储列表中元素的数组的大小。它始终至少与列表大小一样大。当向 ArrayList 添加元素时,其容量会自动增长。除了添加元素具有恒定的摊销时间成本这一事实之外,没有指定增长策略的细节。

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.

待天淡蓝洁白时 2024-10-08 17:08:31

我知道这个问题已经很老了,但是这个链接可能会有所帮助 java arraylist EnsureCapacity not working ,代码添加“Null”值以调整当前大小。

您可以使用ensureSize,而不是纯粹使用ensureCapacity

public static void ensureSize(ArrayList<?> list, int size) {

    list.ensureCapacity(size);
    while (list.size() < size) {
        list.add(null);
    }
}

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

public static void ensureSize(ArrayList<?> list, int size) {

    list.ensureCapacity(size);
    while (list.size() < size) {
        list.add(null);
    }
}
标点 2024-10-08 17:08:31

大多数情况下,不需要“resize()”操作,因为 (a) ArrayList 在添加元素时自动调整大小,并且 (b) 不清楚您将在 ArrayList<> 中存储什么值,例如“null”不太适合有用。例如,在您的情况下,您可能无论如何都需要一个循环来创建 MatrixCell 对象。

对于那些想知道如何调整 ArrayList 大小以使其变小的读者来说,我很困惑为什么 ArrayList 的设计没有使用“resize()”方法。也许是因为新手程序员可能会看到该方法,然后没有意识到 ArrayList<> 的作用。自动调整大小。

在 Java 中,这个习惯用法可以减少 ArrayList<> 的大小:

    list.subList(n,list.size()).clear();

它之所以有效,是因为“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<>:

    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<>'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文