• 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

Can we decrease the size of the ArrayList ?.

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


I have seen some pages saying that ArrayList size can be increased and decreased dynamically at run time.


I do accept that Array List it self increases its size whenever the objects reaches its full capacity.

Here my doubt is how we can decrease the size of the Array List ?.


Thanks
Praveen
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList (or any List) is a dynamic data structure. So it doesnt have any fixed size. It increases when ever you add an element and might decrease when ever you remove an element (this depends on the Garbage collection activity).
 
Praveen Kumar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot.
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, read API

http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html#ensureCapacity%28int%29
http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html#trimToSize()

EDIT: And I do not think garbage collection activity has anything to do with it. If the empty locations are part of the ArrayList then garbage collector will not try to free them. So if you do not use the trimToSize you have no way of knowing if the locations are free to garbage collect or not. It is up to ArrayList implementation how it handles its empty locations.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ilari Moilanen wrote:
EDIT: And I do not think garbage collection activity has anything to do with it. If the empty locations are part of the ArrayList then garbage collector will not try to free them. So if you do not use the trimToSize you have no way of knowing if the locations are free to garbage collect or not. It is up to ArrayList implementation how it handles its empty locations.



You are right. I was thinking in terms of the Size on heap- considering the ArrayList has Objects as its elements. Totally not in context of the Number of elements in the List.
 
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

Praveen Kumar wrote:I do accept that Array List it self increases its size whenever the objects reaches its full capacity.


You seem to be confused about two different concepts - the ArrayList's size and its capacity.

The size of the ArrayList changes whenever you add or remove an element to or from it. So you can decrease the size by simply removing one or more elements.

Internally, for efficiency reasons, the ArrayList allocates some space to store new elements (anticipating that you're going to add them later). This reserved space is the capacity. How the capacity grows and shrinks depends on the implementation details of class ArrayList, and is something you normally don't need to be concerned with. If you want to know exactly how it works, you can lookup the source code of ArrayList in the file src.zip that's in your JDK installation directory.
 
reply
    Bookmark Topic Watch Topic
  • New Topic