how-to

Collections

How to use ArrayList

java.util.ArrayList is very useful class for storing objects. It is newest replacement for Vector class. ArrayList supports null values and duplicates.

/* -- create ArrayList */

ArrayList ar = new ArrayList();

/* -- add items */

ar.add( "One" );
ar.add( "Two" );
ar.add( "Two" );
ar.add( null );
ar.add( "Three" );
ar.add( null );

/* -- get and write second item */

System.out.println( ar.get( 1 ).toString() );

/* -- write all items */

System.out.println( ar.toString() );

Output is:

Two
[One, Two, Two, null, Three, null]