Wednesday, November 29, 2017

JAVA - How to use ArrayList

Class java.util.ArrayList is very useful class for storing objects. It is replacement for Vector class. ArrayList supports null values and duplicates.
/* -- ArrayList creating  */

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]

No comments:

Post a Comment