ArrayList : Java Glossary
home A words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
ArrayList
ArrayList is like Vector without the synchronisation overhead. In JDK 1.1 ArrayList is about 3-4 times faster than Vector, though with JDK 1.1.4+ the difference is not so pronounced. (Rolling your own array is about 40 times faster than Vector.) For compatibily with versions of Java prior to 1.1, you need to use Vector instead. Don't use Vector in new code. It has confusing redundant methods, and the synchronisation is excessive and implicit.
Adding Elements ArrayList ⇔ array
ArrayIndexOutOfBoundsException Thread Safety
GrowingArrayList Generics
Removing Elements Learning More
Trimming Links

Adding Elements to An ArrayList

// adding an element to the middle of a list
arrayList.add( i, object );

// adding an element to the end of a list
arrayList.add( object );
actually inserts, sliding existing elements up to make room. As you might guess, this is an expensive operation when there are many elements.

ArrayIndexOutOfBoundsException

ArrayList will often give you ArrayIndexOutOfBoundsException or IndexOutOfBoundsException. There are many ways to generate one.

GrowingArrayList

Unlike an array where you get the size with v.length, with an ArrayList you use v. size();

With an ordinary ArrayList you can’t set elements until you have grown the ArrayList sufficiently first with add. If you try it, you will get an ArrayIndexOutOfBoundsException or IndexOutOfBoundsException.

If you want an ArrayList that grows automatically when you use a set index too big, here is the complete source code for it.

Removing Elements From An ArrayList

Trimming ArrayLists

When you are done removing elements, you have logically removed them, but the RAM they had allocated is still nailed down, ready to use for future growth. If you want to reclaim it, you must use ArrayList. trimToSize(), which allocates a new internal array just the right size, and copies everything over from the new one, and discards the old one to be later garbage collected. You don’t normally want to use trimToSize unless you know the ArrayList is not going to grow again, any time soon. It is a relatively costly operation, so you don’t want to do it just to reclaim a handful of bytes. You might use it after a massive pruning, or after you were sure nothing new was coming.

When I am absolutely sure there are no new elements coming for the ArrayList, I typically use toArray and use the much faster raw array from then on. I think of ArrayLists mainly as tools to build a Collections, and I think of arrays as tools to process them. However, most other programmers are not nearly so keen as I am on naked arrays. They prefer to do everything with ArrayLists. when you use generics and for:each it is relatively easy to flip back and forth between each style.

ArrayList.add or ArrayList. addAll will not discard duplicates. To do that you would need to check with ArrayList.contains before adding, or create a HashSet which naturally dedups, then use ArrayList.addAll to effectively convert it to an ArrayList.

ArrayList ⇔ array

Thread Safety of ArrayLists

To get the thread safety of Vector in ArrayList wrap it like this:
// making an ArrayList threadsafe
ArrayList a = new ArrayList( 100 );
Collection threadSafeList = Collections.synchronizedCollection( a );

Generics with ArrayLists

Using an ArrayList with generics:

Learning More

Sun’s Javadoc on the ArrayList class : available:
Sun’s Javadoc on the Arrays class : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.18] The information on this page is for non-military use only.
You are visitor number 97,145. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/arraylist.html J:\mindprod\jgloss\arraylist.html