ArrayList: how does the size increase?

后端 未结 19 850
既然无缘
既然无缘 2020-11-29 18:11

I have a basic question on Java ArrayList.

When ArrayList is declared and initialized using the default constructor, memory space for 10 el

19条回答
  •  佛祖请我去吃肉
    2020-11-29 18:26

    (oldSize * 3)/2 + 1

    If you are using default constructor then initial size of ArrayList will be 10 else you can pass the initial size of array while creating the object of ArrayList.

    Example: In case default constructor

    List list = new ArrayList<>();
    list.size()
    

    Example: In case parameterized constructor

    List list = new ArrayList<>(5);
    list.size()
    

提交回复
热议问题