jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes  how Arraylist  work internally? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " how Arraylist  work internally?" Watch " how Arraylist  work internally?" New topic
Author

how Arraylist work internally?

priyanaka jaiswal
Ranch Hand

Joined: Jun 03, 2011
Posts: 79
Hi all,

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



Thanks in advance
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.

luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
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

Joined: Sep 22, 2009
Posts: 1
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

Joined: Nov 11, 2010
Posts: 8
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

Joined: Oct 13, 2005
Posts: 38020
    
  22
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how Arraylist work internally?
 
Similar Threads
Are Arrays are better than ArrayList or in any aspect?
Java Collections - Internal Working
web & application server
Internal working of Context & InitialContext...?
how arraylist increases it size automatically?