5

我经常在我的 Android 应用程序中使用列表。现在我正在创建一个 Twitter 页面,其中列出了最多 50 条用户的“推文”。

我有一个这样定义的列表:

List<Tweet> tweets = new ArrayList<Tweet>(MAX_TWEETS);

whereTweet是一个包含 twitter 更新信息(文本、日期、用户名等)的自定义对象类型,并且MAX_TWEETS是一个常量整数值(50)。

问题:

如果有的话,设置 this 的初始容量有什么好处List

当我知道我的清单会这么小时,我是否应该费心设置容量?我什么时候应该/不应该设置容量?

4

8 回答 8

3

设置初始容量可以提高填充列表时的性能,并且如果您从不向列表中添加超过该数量的项目,它还可以减少列表的内存占用。

The memory footprint of a List that has grown, and might have a backing array that is larger than the number of items stored can be reduced by invoking trimToSize()

于 2011-05-25T18:16:41.670 回答
3

By default, in Java 6, the size of a List is 10. That is, the system creates ten memory slots in the underlying Array. If you try adding the 11th element, only the Array copy is created. Providing a size improves performance.

于 2011-05-25T18:17:14.287 回答
2

设置这个 List 的初始容量有什么好处?

它将在内部分配该大小的内存,当它超过该大小时,它将重新分配内存并将 . 如果我们提供适当的初始容量,我们可以在重新调整溢出时节省一些 cpu 周期。

于 2011-05-25T18:16:22.720 回答
2

设置列表的容量只会指定它有多大。这样做的唯一好处是,当您添加超出列表默认大小的项目时,它不必扩展列表。

示例:您将列表设置为 25,默认为 10?(不是 100% 确定)如果您添加 24 个元素,则根本不需要增加列表。如果您将其保留为默认值,它将增长它。

希望这可以帮助

于 2011-05-25T18:16:26.677 回答
2

The initial capacity helps if you know that you will need exactly that amount. It will create a container that will be able to reference MAX_TWEETS items. If you do exceed it, the system will create a new list with twice as many items then copy over the original list (which is common enough in Java applications).

于 2011-05-25T18:16:57.490 回答
2

ArrayList, as the name suggests, is implemented as an array (as opposed to a linked list). By specifying the initial size, you can prevent having to grow the array when adding elements. This is an expensive operation, a new array must be created and then the existing elements copied. So, if you know the max values ahead of time, you should never have to do this.

In reality, if the size of your list is 50 and there is only one instance of this array, the array will only be expanded a few times, so in this case it might not matter. Still, your approach is good in case you change the variable later.

于 2011-05-25T18:17:19.197 回答
2

Default capacity of ArrayList is set to 10 (see jdk 1.6 source). That means array of size 10 will be allocated on creation. If you will be adding element number 11 the capacity will increase to 16. Then increase again once you reach 21.

If you don't expect more than 50 elements the array will resize at most 3 times. Given that small number, it really does not matter much. Set it to 50 if it gives you a piece of mind of saving on array copy.

Actually this is correct formula of size increase:

int newCapacity = (oldCapacity * 3)/2 + 1;
于 2011-05-25T18:21:29.740 回答
2

It will allocate the memory at creation and won't have to copy until you go over that.

But honestly, with only 50 objects, the copying won't take much effort anyway, so I highly doubt you'll see any performance gain at all. But, there's no downside to specifying the size, so you might as well do it.

于 2011-05-25T18:21:44.320 回答