ArrayList class


see Collection interfaceand additionally seeList interface for descriptions and examples of ArrayList's methods.


See Vector class for descriptions of the methods .lastIndexOf(...),.removeRange(...) and.trimToSize( ).They behave the same for ArrayList as described in Vector.



ArrayList is intended to be the unsynchronized List replacement for the older Vector class.


■ Use ArrayList when duplicates or nulls are expected and FIFO order (first-in, first-out) is desired.

i.e. This snippet printsMickey Mickey Minnie Donald in the order in which they were added to the ArrayList:


import java.util.*;


List al = new ArrayList( );

al.add( "Mickey" );

al.add( "Mickey" );

al.add( "Minnie" );

al.add( "Donald" );

System.out.print( al );†††††


■ An ArrayList's index begins with zero.i.e. Using the above example with ArrayListal already existing, we can manipulate al:


System.out.print( al.get( 2 ) );prints Minnie from position 2 (counting 0,1, 2 to Minnie).

al.set( 1, "Pluto" );replaces the second Mickey above with Pluto (counting 0,1 to Pluto).


■ Because it is unsynchronized, you cannot structurally modify an ArrayList if others are using it.Instead, in order to guarantee serial access, obtain a synchronized thread-safe list, backed by your list, from Collections.synchronizedList( .. ).Then manually synchronize on it, and use it for all iterations. i.e. Again using the al example from above, where ArrayListal already exists:


Mickey Mickey Minnie Donaldis printed here from sl.See Iterators


List sl = Collections.synchronizedList( al );

synchronized( sl ) {

††† Iterator i = sl.iterator( );

††† while ( i.hasNext( ) )System.out.print( ) );



If you don't have an ArrayList already existing, "wrap" a new list with a synchronized object using the Collections.synchronizedList method as follows:


List sal = Collections.synchronizedList( new ArrayList( ) );


■ You cannot create an ArrayList directly from an existing array using ArrayList's constructors alone.The constructor public ArrayList( Collection c) only takes another Collection.


To create from an array, you need to insert the Arrays.asList(...) method as the constructor's argument.i.e. In the snippet below, ArrayListal is started with the contents of a pre-existing object array sa.The snippet prints:††††

List length is: 3

List contains: [AAA, BBB, CCC]


import java.lang.reflect.Array;

import java.util.*;


String[ ] sa = { "AAA", "BBB", "CCC" };

ArrayList al = new ArrayList( Arrays.asList( sa ) );

System.out.println( "List length is: " + al.size( ));

System.out.println( "List contains: " + al );


You cannot do a getFirst(...), getLast(...), addFirst(...), addLast(...), removeFirst(...), or removeLast(...) with an ArrayList.It doesnít provide extra methods for easy operation on the listís ends, as LinkedList does.See LinkedList for these methods.


■ Let's say you want to create an array containing the elements in an existing ArrayList. You must use explicit casting, because the toArray(...) method returns an Object type.i.e.


String[ ] sa = ( String[ ] ) al.toArray( new String[ al.size( ) ]);





ArrayList ar = new ArrayList( )Creates an empty list with an initial capacity of ten.

ArrayList ar = new ArrayList( Collection c )Creates a list containing the specified collection's elements.

ArrayList ar = new ArrayList( int initialCapacity )Creates an empty list with the specified initial capacity. This can be useful in avoiding the performance penalty of incremental enlargements.


■ Important ArrayList methods are shown below†† ( see Collection interface and List interfacefor the others )



void .ensureCapacity( minCap )method


■ ArrayLists grow automatically to accommodate new element additions, but this is inefficient. You can offset the performance penalty of doing one-at-a-time adds by increasing the list's capacity in bulk beforehand with this ensureCapacity(int minCapacity) method.i.e.


al.ensureCapacity(10000);increases capacity to 10,000 elements.



void .trimToSize( )(same method as for Vector - see example underVector )