• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why default size of ArrayList is 10

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all sorry for the prevoius post i got that solution Kindly excuse me
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 230
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are we talking about default size or default capacity of arraylist ?
 
Master Rancher
Posts: 4806
72
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 230
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

int newCapacity = (oldCapacity * 3)/2 + 1;
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic