Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how Arraylist work internally?

 
priyanaka jaiswal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

How arraylist work internally which make it possible to increase size depending upon demand?



Thanks in advance
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can go through the source code and see for yourself. You'll find the sources in a file named src.zip in your JDK folder.
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you work out how to copy the contents of an array into a larger array of the same type? The System.arraycopy method may be of use to you.
 
Madan Mohan Reddy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

List is an interface and ArrayList is implementation which supports this...the inner mechanism is like loop to add into object...suppose

List l = new ArrayList();
l.add("ADD");

I never got a thought how it works internally.. but seems to be some looping concept adding data to object..
 
sandy chops
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arraylist, constructs an empty list with the specified initial capacity.
It uses the object[] array buffer to store the elements.
if no initial capacity is specified a empty list with capacity of 10is created.


As new items are added to Arraylist, it checks the array size and if there is empty space it copies it there else a new large array is created. and old array is copied to the new one.
elementData = Arrays.copyOf(elementData, size, Object[].class);

Further details check ArrayList.class.
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madan Mohan Reddy wrote: . . . ...the inner mechanism is like loop to add into object.. . . . but seems to be some looping concept adding data to object..
Welcome to the Ranch

ArrayList does not use a loop for adding or removal, but it does use loops for finding elements.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic