5 Replies Latest reply: Feb 11, 2011 7:44 AM by java4ever RSS

    ArrayList initial capacity

    800965
      Hi again,

      I'm doing another exercise. I need to create an ArrayList with initial capacity. I'm doing this:
              List<Integer> l1 = new ArrayList<Integer>(10);
              System.out.println(l1.size());
      but putting 10 as an argument to the constructor doesn't set the initial capacity to 10, surprisingly. It prints 0. How can I set the initial capacity?

      Thanks,

      PR.
        • 1. Re: ArrayList initial capacity
          Kayaman
          Aardenon wrote:
          List<Integer> l1 = new ArrayList<Integer>(10);
          System.out.println(l1.size());
          but putting 10 as an argument to the constructor doesn't set the initial capacity to 10, surprisingly.
          Unsurprisingly, it does. Also if I remember correctly, 10 is the default initial capacity (although that's probably not specified anywhere).
          It prints 0.
          You're printing the actual size, not the initial capacity.
          Initial capacity just tells how much space there is in the list until it needs to expand (so if you know that you're putting a million objects in the list, you can create it with an initial capacity of a million to avoid the resizing required).
          • 2. Re: ArrayList initial capacity
            YoungWinston
            Aardenon wrote:
            ...but putting 10 as an argument to the constructor doesn't set the initial capacity to 10, surprisingly. It prints 0. How can I set the initial capacity?
            You have, but capacity and size aren't the same thing. Setting capacity says "I will be putting at least n things into this ArrayList" and is used for performance tuning (it saves the ArrayList from having to expand its internal array).

            But until you actually add something the size will be 0.

            Winston

            Edited by: YoungWinston on Feb 11, 2011 12:56 PM

            Too late. :-)
            • 3. Re: ArrayList initial capacity
              java4ever
              List<Integer> l1 = new ArrayList<Integer>(10);
              System.out.println(l1.size());
              but putting 10 as an argument to the constructor doesn't set the initial capacity to 10, surprisingly. It prints 0. How can I set the initial capacity?
              It doesn't not hold any Integer value... the list is empty hence the size method returns 0.

              you need to add elements like
                 l1.add(new Integer(1)); 
              • 4. Re: ArrayList initial capacity
                800268
                RainaV wrote:
                you need to add elements like
                   l1.add(new Integer(1)); 
                Preferably you would use auto boxing:
                list.add(1);
                // or if you don't like autoboxing (might use cached value instead of new instance)
                list.add(Integer.valueOf(1));
                • 5. Re: ArrayList initial capacity
                  java4ever
                     l1.add(new Integer(1)); 
                  Preferably you would use auto boxing:
                  list.add(1);
                  // or if you don't like autoboxing (might use cached value instead of new instance)
                  list.add(Integer.valueOf(1));
                  Thx for pointing that out