Why is vector array doubled?

前端 未结 7 1724
北恋
北恋 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 22:00

    Any multiple is a compromise. Make it too big and you waste too much memory. Make it too small and you waste much time for reallocations and copying. I guess that doubling is there because it works and is very easy to implement. I also saw a proprietary STL-like library that uses 1.5 as multiplier for the same - I guess its developers considered doubling wasting too much memory.

    0 讨论(0)
提交回复
热议问题