Win a copy of Microservices in Action this week in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

ArrayList parameterised constructor  RSS feed

 
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Could anyone tell me what the 3 in the bold text indicate?
 
Marshal
Posts: 6267
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer to JavaDoc's description
 
Liutauras Vilda
Marshal
Posts: 6267
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, had no time to elaborate more.

Don't confuse size and capacity. These are different.

Size defines the number of elements this very List contains.

Capacity term is more relevant to List internals or more precisely, how it allocates space for the elements it is about to hold. ArrayList storage implemented using an array. When you create an ArrayList, by default it allocates 0 capacity, which in turn creates zero length array internally. So, that parameter you were asking, defines what length array is created during ArrayList instantiation.

Makes sense?
 
Liutauras Vilda
Marshal
Posts: 6267
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie pointed out that I was mistaken about the initial capacity of an ArrayList. It is actually 10. Originally I thought it creates capacity of 0, and only when the first element gets attempted to be added, capacity gets increased to 10.
 
Marshal
Posts: 61805
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . only when the first element gets attempted to be added, capacity gets increased to 10.

That sounds a confusing way to do it.
 
Saloon Keeper
Posts: 1174
73
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Campbell Ritchie pointed out that I was mistaken about the initial capacity of an ArrayList. It is actually 10. Originally I thought it creates capacity of 0, and only when the first element gets attempted to be added, capacity gets increased to 10.


I have gone through the code last night so If you create ArrayList with non parameter constructor then initially It just assigns an empty static array of type Object. When you add an element then creates an array of Object with default size 10 else It wont create array of size 10 unnecessarily occupying space.

In the source code of ArrayList It has comment probably of authors which says ( I could be wrong but find It confusing and misleading since DEFAULTCAPACITY_EMPTY_ELEMENTDATA; is an empty array )
But If you go through the code in source code then you will see It creates an empty one dimensional array of type static Object when this class is loaded.

If creates an object of ArrayList with no parameter constructor then It just assigns above already created empty static array of Object to It's instance variable of type transient Object[] elementData; to use It further.
Hence here capacity at the beginning is 0. The moment you add an element using add() method It creates an array of having capacity 10 i.e. default capacity


If you create ArrayList with parameter constructor passing initialCapacity then creates an array of capacity equal to the value passed as initialCapacity.

It's really logical to create an empty static array when class is loaded and initially assign that same array to instance array variable of all objects of ArrayList created with non parameter constructor.
It creates new array with default capacity 10 only when add method is invoked.

This reduced fruitless memory consumption, how? Suppose If they had created array of capacity 10 when you just create object of ArrayList with non parameter constructor then

above code need memory for 100 arrays of type Object having capacity 10

but we haven't even used any of them yet consumed so much memory so they use only one static array at the beginning once you add an element then creates array of default capacity 10.
 
Liutauras Vilda
Marshal
Posts: 6267
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we state that JavaDocs are wrong, or at least misleading?

JavaDocs wrote:public ArrayList()

Constructs an empty list with an initial capacity of ten.

 
Ganesh Patekar
Saloon Keeper
Posts: 1174
73
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In previous post I wrote:I could be wrong but find It confusing and misleading since DEFAULTCAPACITY_EMPTY_ELEMENTDATA; is an empty array


Liutauras Vilda wrote:Can we state that JavaDocs are wrong, or at least misleading?

JavaDocs wrote:public ArrayList()

Constructs an empty list with an initial capacity of ten.


Since I found following information now I'm thinking both are correct in different views ( Couldn't find exact word to use here )


Googled more about ArrayList() default capacity almost all of them says 10 but I came to know a new and interesting thing is Lazy initialization

Lazy initialization Wikipedia wrote:In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.


Hence Java doc explanation might have considered Lazy initialization.  
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!