java arraylist ensureCapacity not working

后端 未结 8 2021
天命终不由人
天命终不由人 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:16

    No, ensureCapacity doesn't change the logical size of an ArrayList - it changes the capacity, which is the size the list can reach before it next needs to copy values.

    You need to be very aware of the difference between a logical size (i.e. all the values in the range [0, size) are accessible, and adding a new element will add it at index size) and the capacity which is more of an implementation detail really - it's the size of the backing array used for storage.

    Calling ensureCapacity should only ever make any difference in terms of performance (by avoiding excessive copying) - it doesn't affect the logical model of what's in the list, if you see what I mean.

    EDIT: It sounds like you want a sort of ensureSize() method, which might look something like this:

    public static void ensureSize(ArrayList<?> list, int size) {
        // Prevent excessive copying while we're adding
        list.ensureCapacity(size);
        while (list.size() < size) {
            list.add(null);
        }
    }
    
    0 讨论(0)
  • 2020-12-03 06:17

    Ensuring capacity isn't adding items to the list. You can only get element 190 or add at element 190 if you've added 191 elements already. "Capacity" is just the number of objects the ArrayList can hold before it needs to resize its internal data structure (an array). If ArrayList had a getCapacity(), then doing this:

    ArrayList<String> a = new ArrayList<String>();
    a.ensureCapacity(200);
    System.out.println(a.size());
    System.out.println(a.getCapacity());
    

    would print out 0 and some number greater than or equal to 200, respectively

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