Results 1 to 2 of 2
Thread: Use ArrayList Constructor...
- 07-17-2007, 04:56 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 26
- Rep Power
- 0
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,
(source code for ArrayList class) Docjar: java/util/ArrayList.javaJava Code:ArrayList test = new ArrayList(int length);
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
- 08-07-2007, 05:37 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
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:
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.Java Code:ArrayList al = new ArrayList(42); System.out.println(al.size());
Greetings.
Similar Threads
-
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
Constructor with enum
By bugger in forum New To JavaReplies: 2Last Post: 01-04-2008, 11:21 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
Constructor Help
By bluegreen7hi in forum New To JavaReplies: 2Last Post: 11-15-2007, 05:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks