ArrayList in java

Today we are going to learn arraylist in java.

ArrayList in java

  • underlying data structure for arraylist is resizable array.
  • ArrayList allows duplicate elements.
  • ArrayList preserves insertion order.
  • Heterogeneous objects can be stored in ArrayList.
  • ArrayList allow null values.
  • ArrayList is part of collection framework.
  • ArrayList is dynamic array in java. Its size can be increased if objects are added and decreased if objects are removed from collection.
  • ArrayList is slower. But it is useful where lots of manipulation is required.
  • Primitive data types(char, int) cannot be used in ArrayList.
  • We can randomly access list using ArrayList.

Java collection framework provides an architecture to store and manipulate group of objects and it includes interfaces, classes and algorithms.

arraylist in java

In the above figure, green boxes represent interfaces and yellow boxes represent implementation classes.

Here’s the java program on arraylist,

import java.io.IOException;
import java.util.*;

public class ArrayListDemo 
{
   public static void main(String[] args) throws IOException 
   {
      // ArrayList size 
      int size = 10;
      // declaring ArrayList with size 
      ArrayList<Integer> al = new ArrayList<Integer>(size);
      // Appending the new element at the end of the list 
      for(int a = 2; a <= size; a++) 
         al.add(a); 

      // print elements 
      System.out.println(al); 

      // remove element at index 3 
      al.remove(3); 

      // printing ArrayList after deleting index 3 
      System.out.println(al); 

      // print elements one by one 
      for(int a = 0; a < al.size(); a++) 
         System.out.print(al.get(a) + " ");
   }
}

Output:

[2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 3, 4, 6, 7, 8, 9, 10]
2 3 4 6 7 8 9 10


Hierarchy of arraylist

arraylist in java

Here is the hierarchy of arraylist class. Arraylist class extends abstractlist. Abstractlist implements list interface.

Also read – hashset in java

And list interface extends collection interface. Collection interface extends iterable interface.

Here methods available in the list interface and collection interface are supported by arraylist class.


Methods supported by arraylist

MethodsDescription
boolean add ( Object e )appends the specified element to the end of this list (optional operation).
boolean addAll(Collection C)appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
void add( int index, Object element )inserts the specified element at the specified position in this list (optional operation).
boolean addAll( int index, Collection C )inserts all of the elements in the specified collection into this list at the specified position (optional operation).
void trimToSize()this method is used to trim the capacity of the instance of the ArrayList to the list’s current size.
boolean remove( Object o )removes the first occurrence of the specified element from this list, if it is present (optional operation).
boolean removeAll( Collection c )removes from this list all of its elements that are contained in the specified collection (optional operation).
boolean retainAll( Collection c )retains only the elements in this list that are contained the specified collection (optional operation).
Object clone()this method is used to return a shallow copy of an ArrayList.
boolean contains( Object o )returns true if this list contains the specified element.
boolean containsAll( Collection c )returns true if this list contains all of the elements of the specified collection.
E get( int index )returns the element at the specified position in this list
int indexOf( Object o )returns the index of the first occurrence of the specified element in this list, or - 1 if this list does not contain the element.
int lastIndexOf( Object o )returns the index of the last occurrence of the specified element in this list, or - 1 if this list does not contain the element.
void clear( )removes all of the elements from this list (optional operation).
boolean isEmpty( )returns true if this list contains no elements.
int size( )returns the number of elements in this list.
List subList( int fromIndex, int toIndex )returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
Iterator iterator( )returns an iterator over the elements in this list in proper sequence.
ListIterator listIterator( )returns a list iterator over the elements in this list (in proper sequence).
ListIterator listIterator( int index )returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
Object[ ] toArray( )returns an array containing all of the elements in this list in proper sequence (from first to last element).
Object[ ] toArray(Object[ ] O)is also used to return an array containing all of the elements in this list in the correct order same as the previous method.

Arraylist constructors

Arraylist belongs to java.util package. Arraylist implements list interface. So whatever methods available in list interface are implemented by arraylist.

Constructors in Arraylist,

  • ArrayList() – creates empty ArrayList object with default initial capacity.
  • ArrayList(c:Collection<? extends E>) – this one is parameterized constructor which accepts collection. It will create the arraylist from any collection object.
  • ArrayList(int initialCapacity) – this accepts “int” value. it creates empty lists with the specified initial capacity.

Reference – Oracle docs