简体   繁体   English

为什么ArrayList 10的默认容量?

[英]Why is the default capacity of ArrayList 10?

I saw the java doc for ArrayList and found that the initial capacity of ArrayList is 10. 我看到了ArrayList的java doc,发现ArrayList的初始容量是10。

 /**
 * Constructs an empty list with an initial capacity of ten.
 */
public ArrayList() {
this(10);
}

I think it would make sense if it were any power of 2, but why 10? 我认为如果它是2的任何力量,但为什么10?

I also checked HashMap's initial capacity, and it's 16 which makes sense. 我还检查了HashMap的初始容量,它是16,这是有道理的。

/**
 * The default initial capacity - MUST be a power of two.
 */
static final int DEFAULT_INITIAL_CAPACITY = 16;

/**
 * Constructs an empty <tt>HashMap</tt> with the default initial capacity
 * (16) and the default load factor (0.75).
 */
public HashMap() {
    this.loadFactor = DEFAULT_LOAD_FACTOR;
    threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
    table = new Entry[DEFAULT_INITIAL_CAPACITY];
    init();
}

Is there any specify reason behind the number 10? 10号后面有没有明确的原因?

The ArrayList is simple growing array. ArrayList是一个简单的增长数组。 When trying to add element, and the buffer size is exceeded, it is simply growing. 当尝试添加元素,并且超过缓冲区大小时,它只是在增长。 So the initial size can be any positive value. 所以初始大小可以是任何正值。

The 1 would be too little. 1会太少了。 Even with a few elements we will have a few resize operations. 即使有一些元素,我们也会进行一些调整大小操作。

The 100 would be a loss of space. 100将是一个空间的损失。

So, the 10 is compromise. 所以,10是妥协。 Why 10 and not 12 or 8? 为什么10而不是12或8? First hint, that the typical use cases were analysed and this is the best fit between lost of performance and lost of space. 首先提示,分析了典型的用例,这是性能损失和空间丢失之间的最佳匹配。 However, I think, seeing the Sun's original code, that it wasn't analysed so deeply and it is an arbitrary 'not too small, not too big' number. 但是,我认为,看到太阳的原始代码,它没有被如此深入地分析,它是一个任意的“不太小,不太大”的数字。

For a List, there is no advantage in having the capacity be a power of two. 对于列表,容量是2的幂是没有优势的。 In fact, there is no real advantage in any specific starting capacity. 事实上,任何特定的起始能力都没有真正的优势。 It has to be large enough to avoid multiple resizing steps for the common case of small lists, and small enough not to waste memory on unused capacity in that same case. 它必须足够大以避免针对小列表的常见情况的多个调整大小步骤,并且足够小以不在相同情况下在未使用的容量上浪费存储器。 10 was probably chosen simply because it falls in the right range to fulfill these requirements and because it's "round". 10可能只是因为它落在适当的范围内以满足这些要求并因为它是“圆形”而被选择。

来自JDK 1.0的Vector的默认初始容量为10,因此当它们在1.2中引入ArrayList时,它可能保持一致。

Completely arbitrary choice. 完全随心所欲。

And there is no reason why power-of-2 makes any more sense here. 并且没有理由为什么2的幂在这里更有意义。 It makes sense in a HashMap, because of how the hashing works. 它在HashMap中是有意义的,因为散列的工作原理。 In fact, it has to be a power of two (according to the comment in the source). 事实上,它必须是两个人的力量(根据来源中的评论)。

Note that java.util.Vector (which is the older brother of ArrayList) also has 10. 请注意,java.util.Vector(它是ArrayList的哥哥)也有10个。

对于默认的元素数,10可能是或多或少的任意数。

Unless there is a comment in the code, we'll never know for sure. 除非代码中有评论,否则我们永远不会知道。 However, I imagine that at some point a Sun engineer has gathered statistics on ArrayList usage over a large number of real-world applications, and determined ... empirically ... that 10 gave roughly the best results on average. 但是,我想,在某些时候,Sun工程师已经在大量实际应用程序中收集了有关ArrayList使用情况的统计数据,并且根据经验确定...... 10平均得出的结果大致相同。 (That's how they tune things like this, the optimizer, the bytecode design and so on.) (这就是他们如何调整这样的东西,优化器,字节码设计等等。)

And, and others pointed out, there is no computational advantage (or disadvantage) in using a size that is a power of two for the size of an ArrayList . 并且,和其他人指出,对于ArrayList的大小,使用2的幂的大小没有计算优势(或缺点)。

ArrayList is just an Array which can grow automatically.. ArrayList只是一个可以自动增长的数组。

Yes..the default size is 10 是..默认大小是10

and I think that there's not much thinking behind this initial/default value.The default value 10 just seems not loo large and also not too small.(May be this could be reason). 而且我认为这个初始/默认值背后没有太多的想法。默认值10似乎不是很大而且也不是太小。(可能这可能是原因)。 What if you exceed the default initial capacity of the array..? 如果超过阵列的默认初始容量怎么办? The next capacity for the array is calculated by- 该阵列的下一个容量是通过以下方式计算的:

New capacity=(current capacity*3)/2+1
So next size would be (10*3)/2+1= 16
And next (16*3)/2+1= 25
And So on...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM