Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By barney

Thread: Use ArrayList Constructor...

  1. #1
    romina is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default Use ArrayList Constructor...

    hi! I have been looking at the source code for the ArrayList class, and I found a constructor that allows you to make an ArrayList of a given length,
    Java Code:
    ArrayList test = new ArrayList(int length);
    (source code for ArrayList class) Docjar: java/util/ArrayList.java

    But whenever I try to use this constructor, a normal ArrayList is made, not the one with a length of 42 that I want.

    So my question is: Does this constructor actually work?

    I'd gladly use an array, but using an ArrayList is part of my assignment.
    Thanks

  2. #2
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    The code you provide a link to doesn't seem to be the code for ArrayList that is inherent to Java. The API for ArrayList specifies the default capacity is 10, where as the code you link to sets the default to 16. Are you using the code you link to or the implementation Java provides for you? (It may not make any difference to this problem however)

    Are you using the ArrayList.size() method to determine if the length is 42? If so, you should find that the size method reports only how many items are in the current arraylist, not how many it can hold.

    Thus, the following should always print 0:
    Java Code:
    ArrayList al = new ArrayList(42);
    System.out.println(al.size());
    ArrayList seems to act a lot like a Vector, which means it's capacity can grow, so my feeling is the initial capacity should be of minimal importance.
    Greetings.
    BimalChandraMalik likes this.

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  2. Constructor with enum
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 12:21 PM
  3. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 10:01 AM
  5. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 06:44 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
  •