Java ArrayList.trimToSize() 方法

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

ArrayList.trimToSize() method

javaperformancearraylist

提问by Cataclysm

Can I use ArrayList.trimToSize()method in dynamic arrayList ?

我可以在动态 arrayList 中使用ArrayList.trimToSize()方法吗?

  1. If I use it , what will be happened ?
  2. Can I get any benefits on using this method on dynamic ArrayList.
  3. In which case, I should use this method.
  1. 如果我使用它,会发生什么?
  2. 在动态 ArrayList 上使用此方法有什么好处吗?
  3. 在这种情况下,我应该使用这种方法。

Thanks in advance.

提前致谢。

采纳答案by Jeroen Vannevel

From the docs you link yourself:

从您链接自己的文档中:

Trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.

将此 ArrayList 实例的容量修剪为列表的当前大小。应用程序可以使用此操作来最小化 ArrayList 实例的存储空间。

Internally an ArrayListstores an array that holds all the items. At certain moments the ArrayListwill "expand" this array by copying all values into a larger array. This happens whenever an item is being added and the required capacity is bigger than the current one. What happens at this point is the following line of code:

在内部ArrayList存储一个包含所有项目的数组。在某些时候,它ArrayList会通过将所有值复制到更大的数组中来“扩展”这个数组。每当添加项目并且所需容量大于当前容量时,就会发生这种情况。此时发生的是以下代码行:

int newCapacity = oldCapacity + (oldCapacity >> 1);
elementData = Arrays.copyOf(elementData, newCapacity);

In essence this will create a new array that is 1.5 times the size of the current one. What this method does is resize the internal array so that it has no empty space left.

本质上,这将创建一个新数组,其大小是当前数组的 1.5 倍。此方法的作用是调整内部数组的大小,使其没有剩余空间。

  1. Nothing will happen that's visible to you. You won't lose any data, it's just smaller backing array. Adding data to the arraylist will again expand the array normally.

  2. I assume you mean a "normal" ArrayList. If you use this, you will reduce the memory used but it will also be futile if you will still add data after that AND it is pretty useless for small lists. If you have an ArrayListof many, many items and you're sure you don't want to add anymore then you can call this method to reduce some memory footprint.

  3. See above. I don't think it's very likely you'll ever use this.

  1. 不会发生任何你看得见的事情。您不会丢失任何数据,它只是较小的后备阵列。将数据添加到数组列表将再次正常扩展数组。

  2. 我假设你的意思是“正常” ArrayList。如果您使用它,您将减少使用的内存,但如果您在此之后仍然添加数据,它也将是徒劳的,并且对于小列表来说非常无用。如果您有ArrayList许多项目并且您确定不想再添加,那么您可以调用此方法来减少一些内存占用。

  3. 看上面。我不认为你很可能会使用这个。

trimToSize()from the source:

trimToSize()从来源:

public void trimToSize() {
    modCount++;
    int oldCapacity = elementData.length;
    if (size < oldCapacity) {
        elementData = Arrays.copyOf(elementData, size);
    }
}

回答by Marco13

Well, that's simple:

嗯,这很简单:

  1. The ArrayList will be trimmed to its current size
  2. Yes, you can minimize the storage of an ArrayList instance
  3. When you want to trim the ArrayList and minimize its storage
  1. ArrayList 将被修剪为当前大小
  2. 是的,您可以最小化 ArrayList 实例的存储空间
  3. 当您想要修剪 ArrayList 并最小化其存储时

But seriously: There are few occasions where this method should be called (I personally have neverused it). It's related to how an ArrayList is implemented: As the name suggests, the ArrayList internally uses an array to store the data. When you add new elements to the ArrayList, the size of the array is increased as needed. When you add 1000000 elements to the ArrayList, then the internal Array will have a .length of at least (!) 1000000. When you afterwards remove 999999 elements from the ArrayList, then the internal array will stillhave a .length of at least 1000000. The call to trimToSizewill then make sure that the internal array only has the required size (1, in this case). But again: This is hardly ever necessary or beneficial. You should usually not work on ArrayListinstances anyhow, but on the (more general) Listinterface.

但是说真的:这个方法应该被调用的场合很少(我个人从未使用过它)。它与 ArrayList 的实现方式有关:顾名思义,ArrayList 内部使用一个数组来存储数据。当您向 ArrayList 添加新元素时,会根据需要增加数组的大小。当您向 ArrayList 添加 1000000 个元素时,内部 Array 的 .length 将至少为 (!) 1000000。当您之后从 ArrayList 中删除 999999 个元素时,内部数组的 .length仍将至少为 1000000 . 然后调用trimToSize将确保内部数组只有所需的大小(在这种情况下为 1)。但同样:这几乎没有必要或有益。你通常不应该工作ArrayList无论如何,但在(更通用的)List界面上。