Logic used in ensureCapacity method in ArrayList

前端 未结 2 1112
花落未央
花落未央 2021-02-09 09:52

I was going through the source code of ArrayList. I came across the method ensureCapacity() which increases the capacity of the data array used internally. In that, the new capa

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-09 10:51

    It grows the list by 50% every time it is called, to prevent allocating the world too soon. The + 1 is meant to catch situations where the list was inititalised with a size of 1 :)

提交回复
热议问题