Why is vector array doubled?

前端 未结 7 1753
北恋
北恋 2020-12-02 21:27

Why does the classic implementation of Vector (ArrayList for Java people) double its internal array size on each expansion instead of tripling or quadrupling it?

7条回答
  •  我在风中等你
    2020-12-02 21:41

    Personally, I think its an arbitriary choice. We could use base e instead of base 2 (instead of doubling just multiple size by (1+e).)

    If you are going to be adding large amounts of variables to the vector then it would be advantageous to have a high base (to reduce the amnt of copying you will be doing.) On the flip side if you need to be storing only a few members on avg, then a low base will be fine and reduce the amount of overhead, hence speeding things up.

    Base 2 is a compromise.

提交回复
热议问题