<p>CPython的相关代码是<a href="https://hg.python.org/cpython/file/tip/Objects/listobject.c#l42" rel="nofollow">here</a>:</p>
<pre><code>/* This over-allocates proportional to the list size, making room
* for additional growth. The over-allocation is mild, but is
* enough to give linear-time amortized behavior over a long
* sequence of appends() in the presence of a poorly-performing
* system realloc().
* The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
*/
new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
/* check for integer overflow */
if (new_allocated > PY_SIZE_MAX - newsize) {
PyErr_NoMemory();
return -1;
} else {
new_allocated += newsize;
}
</code></pre>
<p>所以不,它的尺寸不会增加一倍。如果追加/扩展将导致列表的长度为<code>newsize</code>,则支持数组的分配内存将增长到大约该长度加上八分之一。你知道吗</p>
<p>对于<a href="http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/tip/src/share/classes/java/util/ArrayList.java#l204" rel="nofollow">Java OpenJDK 7 ArrayLists</a>:</p>
<pre><code>/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
</code></pre>
<p>所以需要额外的一半容量。你知道吗</p>
<p>请使用<code>ArrayList</code>而不是管理自己的调整大小数组。你知道吗</p>