Java 有没有办法在声明数组列表后设置它的大小?

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

Is there a way of setting the size of an arraylist after declaring it?

javaarraysarraylist

提问by answerSeeker

For example, in a traditional array, i would declare an array like this:

例如,在传统数组中,我会像这样声明一个数组:

int array[];

then, i would later initialize it like this

然后,我稍后会像这样初始化它

array = new int[1000];

in an arraylist i am trying to do the same but I have only been able to initialize it while declaring it like below.

在数组列表中,我试图做同样的事情,但我只能在如下声明它时对其进行初始化。

ArrayList<String> array = new ArrayList<>(1000);

it's almost the same as

这几乎和

int[] array = new int[10000];

So I would like to know if there's a way to initialize an arraylist to for example 1000 after it's been declared in a separate statement.

所以我想知道是否有办法在单独的语句中声明数组列表后将其初始化为例如 1000。

采纳答案by azz

You can use ensureCapacity(int)

您可以使用 ensureCapacity(int)

ArrayList<Integer> al = new ArrayList<>();

al.ensureCapacity(1000);

It is important to note that array lists WILL dynamically resize themselves though.

重要的是要注意数组列表会动态调整自己的大小。

So I would like to know if there's a way to initialize an arraylist to for example 1000 after it's been declared in a separate statement.

所以我想知道是否有办法在单独的语句中声明数组列表后将其初始化为例如 1000。

You could always do this, too:

你也可以这样做:

ArrayList<Integer> al;

al = new ArrayList<Integer>(1000);

This is more akin to the regular array initialisation.

这更类似于常规数组初始化。

回答by Jigar Joshi

it is not same as declaring size for array, it is initial size you are passing in

它与声明数组大小不同,它是您传入的初始大小

you can still go beyond 1000runtime in case of Listand not in array

在数组1000List和不在数组中的情况下,您仍然可以超越运行时

ArrayListdynamically will grow the size as required, 1000 here is initial size of array wrapped under ArrayList

ArrayList动态将根据需要增加大小,这里的 1000 是包裹在下面的数组的初始大小 ArrayList

回答by nhgrif

There's no reason to do this besides performance reasons, but you can use the ensureCapacitymethod.

除了性能原因之外,没有理由这样做,但您可以使用该ensureCapacity方法。

ArrayList<String> array = new ArrayList<String>(100);
//code, code, code, ask user size of ArrayList
array.ensureCapacity(someUserInputSize);

回答by Paul Bellora

I would like to know if there's a way to initialize an arraylist to for example 1000 after it's been declared in a separate statement.

我想知道是否有办法在单独的语句中声明数组列表后将其初始化为例如 1000。

You can declare it first, then initialize it later, just like with the array:

您可以先声明它,然后再初始化它,就像使用数组一样:

ArrayList<String> array;

...

array = new ArrayList<>(1000);

But just so you know, the argument of this ArrayList(int)constructor is the initial capacityof the backing array, rather than the size - it's merely an optimization one can do if the expected size of the ArrayListis known. An ArrayListinstantiated this way will still be emptyuntil elements are added.

但正如您所知,此ArrayList(int)构造函数的参数是后备数组的初始容量,而不是大小 - 如果ArrayList已知的预期大小,它只是可以进行的优化。一个ArrayList实例化这种方式仍然是空的,直到添加元素。