java arraylist ensureCapacity not working

后端 未结 8 2077
天命终不由人
天命终不由人 2020-12-03 05:18

Either I\'m doing this wrong or i\'m not understanding how this method works.

ArrayList a = new ArrayList();
a.ensureCapacity(200         


        
8条回答
  •  孤街浪徒
    2020-12-03 06:08

    Adding 190 null entries to an ArrayList reeks of a misuse of the data structure.

    1. Think about using a standard primitive array.

    2. If you require a generics or want more efficient use of space then consider SparseArray or even a Map like a HashMap may be appropriate for your purposes.

提交回复
热议问题