Java ArrayList 调整大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9762145/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:21:48  来源:igfitidea点击:

ArrayList resizing

javaarraylist

提问by Sotirios Delimanolis

I have an ArrayList object for which I know the exact size. Is there any way to specify that the ArrayList should not extend its capacity?

我有一个 ArrayList 对象,我知道它的确切大小。有什么方法可以指定 ArrayList 不应扩展其容量?

List<String> list = null;
int size = getSize(); // gets the exact number of elements I want

list = new ArrayList<String> (size);

for (int i = 0; i < size; i++) {
    list.add("String num: " + i);
}

I don't want the ArrayList to re-size because that takes time which I want to avoid wasting.

我不希望 ArrayList 重新调整大小,因为这需要我想避免浪费的时间。

采纳答案by Ashwinee K Jha

list = new ArrayList<String> (size);

This will create arraylist with 'size' as initial capacity. As long as you don't add more elements than 'size' there will be no resizing.

这将创建以“大小”作为初始容量的数组列表。只要您添加的元素不超过“大小”,就不会调整大小。

Also please be sure that this really takes time in your application. Unless you have profiled and identified this as issue, you will not gain much by randomly optimizing the code.

另外请确保这在您的应用程序中确实需要时间。除非您已对此进行分析并将其确定为问题,否则通过随机优化代码不会获得太多收益。

回答by Jon Skeet

The ArrayListwon't resize if you don't add more elements than it has capacity for. You've created the list with the right capacity, so it should be fine.

ArrayList如果您不添加超过其容量的元素,则不会调整大小。您已经创建了具有正确容量的列表,所以应该没问题。

You couldcreate a list which threw an exception if you tried to exceed the original capacity, but it's not clear why that would be useful to you here.

如果您试图超过原始容量,您可以创建一个抛出异常的列表,但不清楚为什么这对您有用。

回答by Faraway

If you know the exact size, and it will not be extended in the future, then why don't you just use String arrays.

如果您知道确切的大小,并且将来不会扩展它,那么您为什么不只使用 String 数组。

String[] strArray=new String[size];

回答by Francisco Spaeth

What you could do to limit an ArrayListwould be to override the ensureCapacity(int minCapacity)method like in the following sample:

您可以做些什么来限制 anArrayList将覆盖ensureCapacity(int minCapacity)以下示例中的方法:

public static class MyArrayList<T> extends ArrayList<T> {

    @Override
    public void ensureCapacity(int minCapacity) {
        if (minCapacity > 10) {
            throw new IllegalArgumentException();
        }
        super.ensureCapacity(minCapacity);
    }

}

A small test could be done with the following code:

可以使用以下代码完成一个小测试:

public static void main(String[] args) {
    MyArrayList<String> items = new MyArrayList<String>();

    for (int i = 0; i < 15; i++) {
        try {
            items.add("itm " + i);
            System.out.println("succeeded to insert " + i);
        } catch (IllegalArgumentException e) {
            System.out.println("not able to insert " + i);
        }
    }

    System.out.println("items are: " + items);
}

This will print:

这将打印:

succeeded to insert 0
succeeded to insert 1
succeeded to insert 2
succeeded to insert 3
succeeded to insert 4
succeeded to insert 5
succeeded to insert 6
succeeded to insert 7
succeeded to insert 8
succeeded to insert 9
not able to insert 10
not able to insert 11
not able to insert 12
not able to insert 13
not able to insert 14
items are: [itm 0, itm 1, itm 2, itm 3, itm 4, itm 5, itm 6, itm 7, itm 8, itm 9]