ArrayList Class

ArrayList
  • Uses a dynamic array for storing the elements. It extends AbstractList class and implements List interface.
  • ArrayLists are created with an initial size, when this size is exceeded, it gets enlarged automatically.
  • Can contain duplicate elements.
  • Maintains insertion order.
  • Not synchronized.
  • Random access because array works at the index basis.
  • Manipulation slows because a lot of shifting needs to be occurred if any element is removed from the array list.
  • null insertion is possible.
  • Heterogeneous objects are allowed.
Constructors
1. ArrayList a=new ArrayList();

Creates an empty ArrayList object with default initial capacity “10” if ArrayList reaches its max capacity then a new ArrayList object will be created with

             New capacity=(current capacity*3/2)+1
2. ArrayList a=new ArrayList(int initialcapacity);

Creates an empty ArrayList object with the specified initial capacity.

3. ArrayList a=new ArrayList(collection c);

Creates an equivalent ArrayList object for the given Collection that is this constructor meant for inter conversation between collection objects. That is to dance between collection objects.

package com.ashok.collections;

import java.util.*;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class MyArrayList {
   public static void main(String[] args) {
      ArrayList a=new ArrayList();
      a.add("Ashok");
      a.add(10);
      a.add("Ashok");
      a.add(null);
      System.out.println(a);//[Ashok, 10, Ashok, null]
      a.remove(2);
      System.out.println(a);//[Ashok, 10, null]
      a.add(2,"Vinod");
      a.add("Kimu");
      System.out.println(a);//[Ashok, 10, Vinod, null, Kimu]
   }
}

Note

In every collection class toString() is overridden to return it’s content directly in the following format.

[object1, object2, object3, ......]
  • Usually we can use collection to hold and transfer objects from one tier to another tier. To provide support for this requirement every Collection class already implements Serializable and Cloneable interfaces.
  • ArrayList and Vector classes implements RandomAccess interface so that any random element we can access with the same speed. Hence ArrayList is the best choice of “retrival operation”.
  • RandomAccess interface present in util package and doesn’t contain any methods. It is a marker interface.
package com.ashok.collections;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.RandomAccess;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public static void main(String[] args) {
      ArrayList arrayList = new ArrayList();
      LinkedList linkedList = new LinkedList();

      System.out.println(arrayList instanceof Serializable); // true
      System.out.println(linkedList instanceof Cloneable); // true
   
      System.out.println(arrayList instanceof RandomAccess); // true
      System.out.println(linkedList instanceof RandomAccess); // false
   }
}
ArrayList Vs Vector
1. Synchronization

ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same time. For example if one thread is performing an add operation on ArrayList, there can be an another thread performing remove operation on ArrayList at the same time in a multithreaded environment.

While Vector is synchronized. This means if one thread is working on Vector, no other thread can get a hold of it. Unlike ArrayList, only one thread can perform an operation on vector at a time.

2. Resize

Both ArrayList and Vector can grow and shrink dynamically to maintain the optimal use of storage, however the way they resized is different. ArrayList grow by half of its size when resized while Vector doubles the size of itself by default when grows.

3. Performance

ArrayList gives better performance as it is non-synchronized. Vector operations gives poor performance as they are thread-safe, the thread which works on Vector gets a lock on it which makes other thread wait till the lock is released.

How to get synchronized version of ArrayList object

Collections class defines the following method to return synchronized version of List.

public static List synchronizedList(list l);

 Similarly we can get synchronized version of Set and Map objects by using the following methods.

 1. public static Set synchronizedSet(Set s);
 2. public static Map synchronizedMap(Map m); 
  • ArrayList is the best choice if our frequent operation is retrieval.
  • ArrayList is the worst choice if our frequent operation is insertion (or) deletion in the middle because it requires several internal shift operations.
ArrayList Class


Scroll to top