aspose file tools*
The moose likes Java in General and the fly likes why default size of ArrayList is 10 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "why default size of ArrayList is 10" Watch "why default size of ArrayList is 10" New topic
Author

why default size of ArrayList is 10

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear all
As we know the default size of ArrayList is 10,and when we add any element its size gets incremented by one and allocates the memory for that value and copy the existing one to newly allocated memory.so what what does it mean is the 10 allocated memory leave blank or after the 1oth element has been completed it gets resized


Creativity is nothing but Breaking Rules
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

ArrayList uses an array to store the elements. Arrays have a fixed size. The array that ArrayList uses has to have a default size, obviously. 10 is probably a more or less arbitrary number for the default number of elements. When you create a new ArrayList with nothing in it, then ArrayList will have made an array of 10 elements behind the scenes. Ofcourse those 10 elements are all null.

Every time the array is full, ArrayList creates a new, larger array and copies the elements from the old array to the new array. You don't want that to happen every time that you add an element to the ArrayList (copying the array takes time, especially if the array is large), so ArrayList does it in steps of 10 or so.

If you want to know exactly how it works, look for the file src.zip in your JDK installation directory. Open it and lookup the source code for java.util.ArrayList in there.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Jesper Young wrote:so ArrayList does it in steps of 10 or so.

Actually, capacity is increased by about 50%.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296


I had gone through the documentation of Java API of ArrayList.suppose the size of is 10 and when i want to add 11 element they are adding directly with out incrementing the capacity by 50%
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear all sorry for the prevoius post i got that solution Kindly excuse me
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

ensureCapacity(11) does not mean that the array will have a size of 11. It only means that the array will have a size of at least 11.
Santosh Kumar Nayak
Ranch Hand

Joined: Aug 02, 2011
Posts: 93
Are we talking about default size or default capacity of arraylist ?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
The original poster appears to be talking about both capacity and size, mixing them up. This is confusing; please use terminology carefully. For a newly created arrayList, the default capacity is 10, but the size is 0. Each time you add an element, the size increases by 1, and the capacity usually stays the same. When it does need to increase, the capacity increases by about 50%,
Santosh Kumar Nayak
Ranch Hand

Joined: Aug 02, 2011
Posts: 93
So you mean the capacity becomes 15 for the first time when the capacity increases ?

Is there any formula for the same ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Santosh Kumar Nayak wrote:Is there any formula for the same ?


Yes. He just told you what it is: new size = old size * 1.5. Although that's implementation- and version-specific.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
It is not quite increase by 50%. otherwise a 1‑capacity List would never enlarge.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:It is not quite increase by 50%. otherwise a 1‑capacity List would never enlarge.


Ha! Good point!

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Which is why I said about 50%. It's an approximation. And there are other corner cases as well, dealing with overflow for example. In the case of a 1-capacity list, "not quite increase by 50%" is wrong, of course - it necessarily increases by more than 50%. I suppose we could enumerate all the details of this, implementation-specific as they are, but those details are of course available in the source code to those who feel they need them.
shivdayal Kushwaha
Greenhorn

Joined: Feb 07, 2009
Posts: 4

int newCapacity = (oldCapacity * 3)/2 + 1;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why default size of ArrayList is 10