How to get the capacity of the ArrayList in Java?

后端 未结 9 850
灰色年华
灰色年华 2020-12-05 07:47

Its known that Java ArrayList is implemented using arrays and initializes with capacity of 10 and increases its size by 50% . How to get the current ArrayList capacity not t

相关标签:
9条回答
  • 2020-12-05 08:08

    You can get the current capacity of an ArrayList in Java using reflection. Here is an example:

    package examples1;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.lang.reflect.Field;
    
    public class Numbers {
    
        public static void main(String[] args) throws Exception {
            List<Integer> numbers = new ArrayList<>();
            numbers.add(1);
            System.out.println(getCapacity(numbers));
        }
    
        static int getCapacity(List al) throws Exception {
            Field field = ArrayList.class.getDeclaredField("elementData");
            field.setAccessible(true);
            return ((Object[]) field.get(al)).length;
        }
    }
    

    This will output: 10

    Notes:

    1. getCapacity() method modified from the original at http://javaonlineguide.net/2015/08/find-capacity-of-an-arraylist-in-java-size-vs-capacity-in-java-list-example.html
    2. Note that the default capacity of 10 is granted after the first add to the list. If you try this before adding, you will get an output of 0
    3. To force a capacity without adding, pass it in the constructor like so:

      List<Integer> numbers = new ArrayList<>(20);
      
    0 讨论(0)
  • 2020-12-05 08:08

    The whole point of using ArrayList is to dynamically add new element, So there is no specific method to get the capacity of the ArrayList.

    Every time we add an element dynamically causes reallocation and since reallocation is costly in terms of time, preventing reallocation improves performance and hence you can manually increase the capacity of ArrayList by calling ensureCapacity() but again you can not find out the capacity of the ArrayList.

    0 讨论(0)
  • 2020-12-05 08:10

    Default capacity of ArrayList is 10.once the max size is reached,new capacity will be:

    new capacity=(currentcapacity*3/2)+1.

    0 讨论(0)
  • 2020-12-05 08:13

    Don't remember if it has but you could do it yourself by looking at the source code of ArrayList. Java developers should take advantage of the source code bundled with the SDK.

    0 讨论(0)
  • 2020-12-05 08:15

    I just checked out the sun documentation on the ArrayList class, and the only method I saw that related to the capacity was ensureCapacity(int minCapacity), which is not exactly what you want. Good luck!

    0 讨论(0)
  • 2020-12-05 08:19

    I don't think this is possible. What is your use case? I believe C# ArrayLists have a .capacity property, but the Java ArrayList class doesn't expose this information.

    You have the constructor that takes an initial capacity argument, and you have the ensureCapacity() method which you could use to reduce the amount of incremental reallocation.

    You also have the trimToSize() method you can use if you are really worried about memory usage.

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