Results 1 to 13 of 13
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default Initial capacity of ArrayList

    I have declared an ArrayList and want to know its initial capacity.

    Java Code:
    ArrayList a = new ArrayList();
    How to do it. ArrayList does not provide capacity() method as provided by Vector class.

  2. #2
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    See if you get the answer here..


    XML Code:
    http://www.java-forums.org/new-java/1812-use-arraylist-constructor.html

  3. #3
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks for the link but it does not address my problem.
    I want to know the default capacity of an ArrayList. This is important to me because when ArrayList grows, its an expensive operation.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    The default of initial capacity of ArrayList is 10, you can see it in the ArrayList source code.

  5. #5
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks wsaryada.

    I cannot find capacity() method for getting the capacity of the ArrayList as in Vector.

    How can I confirm this? Please give reference.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    You can look in the ArrayList.java source code itself. It defined in the default constructor as follow:

    Java Code:
       /**
         * Constructs an empty list with an initial capacity of ten.
         */
        public ArrayList() {
    	this(10);
        }
    You can find the source code in your JDK folder, in my PC it is C:\Program Files\Java\jdk1.6.0_03\src.zip

  7. #7
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Great.
    Thanks a dozen. Can we change this?? I mean are we allowed to change this??

  8. #8
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    Of course you can change the initial capacity of ArrayList. When creating the array list just pass the initial capacity you want in the constructor.

    Java Code:
    List list = new ArrayList(20);
    This constructor will set the initial capacity of your array list to 20.

  9. #9
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Yes. But can I change the default capacitiy of the ArrayList by changing ArrayList.java?

  10. #10
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    The constructor is the only way to change it from what I know. Other JDK implementation could have a different initial capacity, such a GNU JDK have 16 as the initial capacity. But again, passing the initial capacity through constructor will be enough.

  11. #11
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    Default

    Using constructor is the easy way to do it. Why would you like to change the code in ArrayList.java.

  12. #12
    Swadi is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default

    If you are developing your application in eclipse, then start in debugging mode and try to find out the initial size.

    The initial size of both ArrayList and Vector are 10.

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Quote Originally Posted by Swadi View Post
    If you are developing your application in eclipse, then start in debugging mode and try to find out the initial size.

    The initial size of both ArrayList and Vector are 10.

    Yes - but that's now.

    Years and years ago (like when this thread was started and the question asked) it might have been different. The orbital period of the moon around the earth, for instance, is larger now than it was in the distant past.

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  2. Limiting the capacity of a cell of JTable
    By rameshraj in forum Advanced Java
    Replies: 0
    Last Post: 03-24-2008, 03:20 PM
  3. Vector capacity
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 06:58 PM
  4. Replies: 1
    Last Post: 08-13-2007, 04:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •