• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why default size of ArrayList is 10

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15455
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:so ArrayList does it in steps of 10 or so.

Actually, capacity is increased by about 50%.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all sorry for the prevoius post i got that solution Kindly excuse me
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are we talking about default size or default capacity of arraylist ?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not quite increase by 50%. otherwise a 1‑capacity List would never enlarge.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

int newCapacity = (oldCapacity * 3)/2 + 1;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic