简体   繁体   English

调整 ArrayDeque 的大小

[英]Resizing of ArrayDeque

Quote: Default initial capacity of ArrayDeque is 16. It will increase at a power of 2 (24, 25, 26 and so on) when size exceeds capacity.引用: Default initial capacity of ArrayDeque is 16. It will increase at a power of 2 (24, 25, 26 and so on) when size exceeds capacity.

Does this mean it behaves similar to ArrayList ?这是否意味着它的行为类似于ArrayList Each time size exceeds capacity there is new array where older elements are copied to?每次大小超过容量时,都会有新数组将旧元素复制到其中? Can I say internal implementation of ArrayDequeue and ArrayList is array (as their name says)?我可以说ArrayDequeueArrayList内部实现是数组(正如他们的名字所说)吗? Just the resizing differs?只是调整大小不同?

Yes ArrayDeque behaves similarly to ArrayList : Internally it uses an Object array.是的ArrayDeque行为类似于ArrayList :它在内部使用 Object 数组。 If the capacity is not enough, it creates a new, larger array, and copies items from the old array to the new.如果容量不够,它会创建一个新的更大的数组,并将项目从旧数组复制到新数组。

The Java API specification does not require any particular resizing behavior. Java API 规范不要求任何特定的调整大小行为。 In fact the current implementation in OpenJDK doubles the size of the array if it's small (64), otherwise it grows by 50% :实际上, 如果数组很小(64), OpenJDK 中的当前实现会将其大小加倍,否则会增长 50%

    // Double capacity if small; else grow by 50%
    int jump = (oldCapacity < 64) ? (oldCapacity + 2) : (oldCapacity >> 1);

It seems that the"doubling" behavior is approximate: thanks to the "+2" after the first resize the capacity is 16+16+2 = 34. After the second resize it's 34+34+2 = 70. After that the array increases by 50% in every resize.似乎“加倍”行为是近似的:由于第一次调整大小后的“+2”容量为 16+16+2 = 34。第二次调整大小后为 34+34+2 = 70。之后数组每次调整大小增加 50%。

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

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