Tutorials

Java Collection Framework


Java ArrayList


ArrayList is a class that extends AbstractList class and implements List interface in java. 

It uses dynamic array for storing the elements thus gaining upper hand in comparison to Array (which is static in nature and has fixed length).

 

Declaration of ArrayList class


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

Key feature of ArrayList

  1. It implements List interface, RandomAccess interface, Collection interface, Iterable interface, Cloneable interface and Serializable interface.
  2. It is a part of java.util package.
  3. ArrayList can expand and shrink automatically as per the need because of its dynamic array storage capability.
  4. ArrayList is not synchronized (else it would have been roughly equivalent to Vector).
  5. It permits all element, including null.
  6. ArrayList is generally slow while manipulating the elements as during insertion or deletion lot of shifting needs to be done.

Create ArrayList in java

We can use below given constructors to create arraylist in java:

  • ArrayList myList = new ArrayList ();     
  • ArrayList myList = new ArrayList (Collection c);
  • ArrayList myList = new ArrayList (int initialCapacity); 

Simple Example:

 

package javaRadarArrayList;
import java.util.ArrayList;
public class SimpleArrayList {
      public static void main(String[] args) {
            //create Array List
            ArrayList javaRadarList = new ArrayList();

            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            System.out.println(javaRadarList);
      }
}

Now let’s look at different ways in which you can use the ArrayList that you created. click on each link to read about them.

 

ArrayList operations

Traverse ArrayList

Convert ArrayList to LinkedList                          

ArrayList or LinkedList. Which one to use?(Click to see the difference between two and decide)

 

How to synchronize ArrayList?

So far we have learned that ArrayList is non-synchronized but still what to do if we need to synchronize it.

So java provides a solution to synchronize your ArrayList. Use synchronizedList() method provided by Collections utility class.


  Collections.synchronizedList(List list)

 



                                

Please Share this page
Views : 95