• 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

how Arraylist work internally?

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

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



Thanks in advance
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Lookout! Runaway whale! Hide behind this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic