File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Can we decrease the size of the ArrayList ?. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we decrease the size of the ArrayList ?." Watch "Can we decrease the size of the ArrayList ?." New topic
Author

Can we decrease the size of the ArrayList ?.

Praveen Kumar
Ranch Hand

Joined: Nov 06, 2006
Posts: 133
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

Joined: Sep 08, 2007
Posts: 3068
    
  33

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).


Mohamed Sanaulla | My Blog
Praveen Kumar
Ranch Hand

Joined: Nov 06, 2006
Posts: 133
Thanks a lot.
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
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

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: Aug 16, 2005
Posts: 14139
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we decrease the size of the ArrayList ?.