Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we decrease the size of the ArrayList ?.

 
Praveen Kumar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks a lot.
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15367
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic