ArrayList Tutorial


ArrayList Tutorial: ArrayList is a member of collections framework introduced with JDK 1.2. After Vector, it is the most used. It is an array-based data structure (LinkedList is node-based). The elements added are stored internally as an array. It permits duplicates and null values as elements. ArrayList elements can be retrieved with the conventional styles of foreach loop, iterators and indexes.

Unlike an array, when elements are added, the ArrayList size increases when new elements are added.

Following is the class signature

public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, Serializable

Observe, the ArrayList is derived from List. It can make use of all the methods of Collection interface as again List is derived by Collection interface.

Performance with ArrayList: Adding more elements decreases performance considerably. When more are added, a new array with increased capacity is created, the contents of the old array are copied into the new array and old array location is garbage collected. If this repeats number of times, the execution becomes slower. With ensureCapacity(), the storing capacity can be increased at a time, if the required size is known earlier. This style will increase the performance.

Note: In a multithreaded environment, ArrayList is not advised as ArrayList is not thread-safe. Its methods are not synchronized; if multiple threads access at the same time, the data can be inconsistent. For this, Vector is preferable as Vector methods are implicitly synchronized.

A synchronized version of ArrayList can be obtained as follows.

ArrayList al1 = new ArrayList();
List myList = Collections.synchronizedList(al1);

In the above code, myList methods are synchronized. Still, al1 is not synchronized.

Following are the constructors

  1. ArayList(): Creates an ArrayList object with an initial capacity to store 10 elements. The list is empty.
  2. ArayList(int cap): Creates an ArrayList object with an initial capacity to store cap elements. The list is empty. Creating a higher capacity, always, increases the performance.
  3. ArayList(Collection col): Creates an ArrayList object with the elements of Collection col. This style helps to get a new ArrayList with the elements of existing collection class.

ArrayList adds its own methods apart inherited from Collection and List.

Following are the some important methods

  1. void ensureCapacity(int cap): This method increases the existing capacity to cap. At a time increasing the capacity, increases the performance. Remember, the default capacity is 10.
  2. int size(): Returns the number of elements added to the list.

ArrayList Tutorial: 4 programs are given on Arraylist.

  1. ArrayList Methods: In this program, the basic operations on array list are performed like addition, checking the list is empty or not, removing the elements, knowing the index number of an element etc.
  2. ArrayList Iterators: In this example, different styles of retrieving and printing the array list elements are described.
  3. ArrayList Operations: In this program, operations like reversing, sorting, searching, swapping, shuffling, filling, knowing maximum and minimum values are performed. Finally, an array of array lists is created and the elements are printed.
  4. ArrayList Special: In this program, operations like converting array list to array, array list to string, copying elements of one array into another, converting array to array list and finally cloning of an array list are performed.

Leave a Comment

Your email address will not be published.