Array List Class

  • The array list class extends abstract list and implements the list interface.
  • Array list supports dynamic arrays that can grow as needed.
  • Array list a variable length array of object references i.e. an array list can dynamically increase or decreases in size.
  • Array list is created with an initial size when this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.
  • The array list class has three constructors as shown below.
  1. Array list ():    Builds an empty array list.
  2. Array list (collection c):  Builds an array list that is initialized with the elements of the collection c.
  3. Array list (int capacity):  Builds an array list that has the specified initial capacity. The capacity as more elements are added to the array list.
  • The array list class is not synchronized
  • To make the array list class synchronized, we follow a different approach.
  • The following program demonstrates a simple use of the array list. An array list is created and string objects are added to it. The list in then displayed. Some modifications are done on the list and again the list is displayed.

 

Example program:

Import java. Util.*;

Class Array List Demo

{

Public static void main (string args [ ])

{

Array List   al = new Array list (); // creates an empty list

System. Out. Prinln(“ intilize size is :” + al. size());

al .add ( “ IND”);

al .add ( “ USA”);

al .add ( “ UK”);

al .add ( “ CHN”);

al .add ( “ SA”);

al .add ( “ SL”);

al .add ( “KEN ”);

System. Out. Prinln(“ size after additions :” + al. size());

System. Out. Prinln(“ contents of list :” +al);

al .add (2, “ AUS”);

System. Out. Prinln(“ size after additions, contents are :” +al);

al.remove(“KEN”);

al .remove (3);

System. Out. Prinln(“ size of the list  after deletions :” + al. size());

System. Out. Prinln(“ contents of the list:” +al);

}

}

 

Java ArrayList Class Project Help