Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-04-2007, 12:34 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Initial capacity of ArrayList
I have declared an ArrayList and want to know its initial capacity.

Code:
ArrayList a = new ArrayList();
How to do it. ArrayList does not provide capacity() method as provided by Vector class.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-04-2007, 02:11 PM
Member
 
Join Date: Aug 2007
Posts: 22
revathi17 is on a distinguished road
See if you get the answer here..


HTML Code:
http://www.java-forums.org/new-java/1812-use-arraylist-constructor.html
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-04-2007, 02:15 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-04-2007, 05:36 PM
Senior Member
 
Join Date: Jun 2007
Location: Bali, ID
Posts: 102
wsaryada is on a distinguished road
The default of initial capacity of ArrayList is 10, you can see it in the ArrayList source code.
__________________
Website:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
- Blog:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-05-2007, 03:33 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-05-2007, 05:40 PM
Senior Member
 
Join Date: Jun 2007
Location: Bali, ID
Posts: 102
wsaryada is on a distinguished road
You can look in the ArrayList.java source code itself. It defined in the default constructor as follow:

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
__________________
Website:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
- Blog:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-05-2007, 06:25 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Great.
Thanks a dozen. Can we change this?? I mean are we allowed to change this??
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-05-2007, 07:06 PM
Senior Member
 
Join Date: Jun 2007
Location: Bali, ID
Posts: 102
wsaryada is on a distinguished road
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.

Code:
List list = new ArrayList(20);
This constructor will set the initial capacity of your array list to 20.
__________________
Website:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
- Blog:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-05-2007, 07:42 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Yes. But can I change the default capacitiy of the ArrayList by changing ArrayList.java?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-05-2007, 07:46 PM
Senior Member
 
Join Date: Jun 2007
Location: Bali, ID
Posts: 102
wsaryada is on a distinguished road
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.
__________________
Website:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
- Blog:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-06-2007, 02:25 PM
mew mew is offline
Member
 
Join Date: Nov 2007
Posts: 70
mew is on a distinguished road
Using constructor is the easy way to do it. Why would you like to change the code in ArrayList.java.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 01:43 PM
Limiting the capacity of a cell of JTable rameshraj Advanced Java 0 03-24-2008 04:20 PM
Vector Capacity JavaForums Java Blogs 0 12-03-2007 02:10 AM
Vector capacity Java Tip Java Tips 0 11-04-2007 07:58 PM
Initial SessionFactory creation failed escit Database 1 08-13-2007 05:46 AM


All times are GMT +3. The time now is 04:27 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org