java Arraylist 和 Vector 的负载因子?

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

Load factor of Arraylist and Vector?

javacollections

提问by Som

Hi I was trying to find the load factor of Array list and vector but I was not able to find it. I know load factor of HashMap and other Map is 0.75. Can any one help to find me how to check the load factor of Vector and Arraylist.

嗨,我试图找到数组列表和向量的加载因子,但我找不到它。我知道 HashMap 和其他 Map 的负载因子是 0.75。任何人都可以帮助我找到如何检查 Vector 和 Arraylist 的负载因子。

采纳答案by Sid Zhang

I assume you would like to know how ArrayListand Vectorincrease its size.

我想你想知道如何ArrayListVector增加它的大小。

For ArrayList, every time you put an element into it, it will check if the nested array needs to be enlarge its size. If yes, generally, its size will grow with:

对于ArrayList,每次放入元素时,它都会检查嵌套数组是否需要扩大其大小。如果是,一般来说,它的大小会随着:

newCapacity = oldCapacity + (oldCapacity >> 1);

For some special case, for example, add many or huge number of elements, things will be different. Please refer grow(int minCapacity)function in java.util.ArrayListsource code.

对于某些特殊情况,例如添加许多或大量元素,情况会有所不同。请参考源代码中的grow(int minCapacity)功能java.util.ArrayList

Regarding Vector, generally, its size will grow with:

关于Vector,一般来说,它的大小会随着:

newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                         capacityIncrement : oldCapacity);

For some special cases, please refer grow(int minCapacity)in java.util.Vector.

对于一些特殊情况,请参阅grow(int minCapacity)java.util.Vector

回答by Vivek Goel

ArrayList:

数组列表:

  • Initial Capacity:10
  • Load Factor:1 (when the list is full)
  • Growth Rate: current_size + current_size/2
  • 初始容量:10
  • 负载因子:1(当列表已满时)
  • 增长率:current_size + current_size/2

Vector:

向量:

  • Initial Capacity:10
  • Load Factor:1 (when the list is full)
  • Growth Rate: current_size * 2 (if capacityIncrement is not defined) current_size + capacityIncrement (if capacityIncrement is defined during vector initialization)
  • 初始容量:10
  • 负载因子:1(当列表已满时)
  • 增长率:current_size * 2(如果没有定义 capacityIncrement) current_size + capacityIncrement (如果在向量初始化时定义了 capacityIncrement)

回答by Krishan Kumar

ArrayList al = new ArrayList(); for(int i=0; i<=10; i++){ al.add(i+1); } default capacity = 10 in the above example, we want to add 11 elements so new Capacity of ArrayList is int newCapacity = (oldcapacity*3)/2+1 (10*3)/2+1 = 16

ArrayList al = new ArrayList(); for(int i=0; i<=10; i++){ al.add(i+1); } 默认容量= 10 在上面的例子中,我们要添加11个元素所以ArrayList的新容量是int newCapacity = (oldcapacity*3)/2+1 (10*3)/2+1 = 16