Collections

Java ArrayList class

  • Java ArrayList class uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.
  • Java ArrayList class can contain duplicate elements.
  • Java ArrayList class maintains insertion order.
  • Java ArrayList class is non synchronized.
  • Java ArrayList allows random access because array works at the index basis.
  • In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.

Syntax :

ArrayList<String> al=new ArrayList<String>();

Example

import java.util.ArrayList;

public class ArrayListExample {
public static void main(String[] args)
{
ArrayList<String> al=new ArrayList<String>();
al.add("Amol"); 
al.add("Rahul");
al.add("Vaishali"); 
al.add("Renuka"); 
al.add("Prachi"); 
al.add("Deepak"); 
System.out.println(al);
 for (int i=0;i<al.size();i++) 
   System.out.println(al.get(i));
 }
 } 

Important points.

  • To add elements in ArrayList  use method add() e.g. arraylist.add(element).
  • To get size of the ArrayList use method size() e.g. arraylist.size()
  • To get any element of the ArrayList use method get() e.g. arraylist.get(index)