ArrayList in Java

ArrayList in Java

An ArrayList is a dynamic data structure in Java that is part of the Java Collections Framework.

ArrayList in Java is similar to Array but with a crucial difference. Arrays have a fixed size that must be declared at initialization, while ArrayList can dynamically resize themselves. When an ArrayList reaches its capacity, it automatically increases its size by (n + n/2 + 1), where n is the initial capacity of the ArrayList. Whenever an instance of ArrayList in Java is created, by default, the capacity of ArrayList is 10.

The size of the ArrayList grows based on load factor and current capacity. ArrayList in Java expands its capacity after each threshold, which is calculated as the product of the current capacity and load factor of the ArrayList instance.

Class ArrayList

The ArrayList Class extends AbstractList and implements the List interface.

ArrayList is the generic class that has this declaration :

class ArrayList <T> { ..... }

where T specifies the type of object that the list will hold.

Characteristics of ArrayList Class

  • The ArrayList class allows the collection to have duplicate elements.

  • ArrayList class Maintains insertion order.

  • A collection, according to the ArrayList class, is non-synchronized.

  • ArrayList allows random access because the array works on index basis.

  • ArrayList supports dynamic arrays that can grow as needed.

  • Using ArrayList class manipulation is slow because a lot of shifting needs to occur if any element is removed from the collection.

ArrayList Constructor

There are mainly three constructors to build an ArrayList:

  • ArrayList(): It is used to build an empty ArrayList.

  • ArrayList(Collection <? extends T>): It is used to build an ArrayList that is initialized with the elements of the collection c.

ArrayList (int capacity): It is used to build an ArrayList initialized with the specified initial capacity.


/* This program will illustrate the use of simple constructor
 i.e Arraylist()*/
import java.util.*;
public class CreateArrayList{
  public static void main(String [] agrs){
  ArrayList<Integer> numbers = new ArrayList<Integer>();
  numbers.add(6);
  numbers.add(7);
  numbers.add(8);
  System.out.println(numbers);
  }
}
/* 
Output: 
[6, 7, 8]
*/
/*This program will illustrate the creation of an ArrayList collection 
with the existing collection i.e ArrayList(Collection <? extends T>)*/
import java.util.*;
public class CreateArrayListFromCollectionEaxmple{
  public static void main(String [] agrs){
  ArrayList<Integer> numbers = new ArrayList<Integer>();
  numbers.add(6);
  numbers.add(7);
  numbers.add(8);
 //creating another collection initially with numbers collection
  ArrayList<Integer> aList = new ArrayList<Integer>(numbers);
  aList.add(9);
  aList.add(10);
 //printing the lists
  System.out.println(numbers);
  System.out.println(aList);
  }
}
/*
Output: 
[6, 7, 8]
[6, 7, 8, 9, 10]
 */
/* This program will illustrate the creation of an user defined 
ArrayList Collection with specified size i.e ArrayList (int capacity)*/
import java.util.*;
public class person{
private String name;
private int age;
  public person(String name, int age){
  this.name = name;
  this.age = age;
  }
  void printData(){
   System.out.println(name + " , " + age);
  }
  public static void main(String [] args){
      ArrayList<person> list = new ArrayList<person>(6);
       list.add(new person("soniya", 20));
       person p2 = new person("swati", 18);
       person p3 = new person("kundan",17);
       person p4 = new person("madhuri",20);
       person p5 = new person("aakash", 20);
       person p6 = new person("kaif", 20);
       list.add(p2);
       list.add(p3);
       list.add(p4);
       list.add(p5);
       list.add(p6);
       list.forEach(p -> p.printData());
  }
} 
/*
Output: 
soniya , 20
swati , 18
kundan , 17
madhuri , 20
aakash , 20
kaif , 20*/

Creating an ArrayList Object using a User-Defined Object

import java.util.*;
public class Person {
    private String name ;
    private int age;
    public Person(String name , int age){
        this.name = name;
        this.age = age;
    }
    public void printData(){
        System.out.println(name + " " + age);
    }
    public static void main(String [] args){
        ArrayList<Person> plist = new ArrayList<Person>(5);
        plist.add(new Person("Kakashi Hatake",30));
        Person p2 = new Person("Naruto Uzumaki", 18);
        plist.add(p2);
        Person p3 = new Person("Sakura Haruno", 20);
        plist.add(p3);
        Person p4  = new Person("Rock Lee", 23);
        plist.add(p4);
        Person p5 = new Person("lady Sunade", 35);
        plist.add(p5);
        plist.forEach(p -> p.printData());

    }
}
/*
Output: 
Kakashi Hatake 30 
Naruto Uzumaki 18 
Sakura Haruno 20
Rock Lee 23
lady Sunade 35 */

Insertion into a Collection

Methods of Insertion into a Collection are as follows:

  • void add ( int index, E element): It is used to insert an element at the specified position.

  • boolean add(E e): It is used to append the specified element at the end of the list.

  • boolean addAll( Collection <? extends T> c): It is used to append all the elements at the end of the already existing specified collection c to the end of the list, in order that they are returned by the specified collection's iterator.

Program to demonstrate the above Methods usage:

import java.util.*;
class HelloWorld {
    public static void main(String[] args) {
        ArrayList<Integer> primes = new ArrayList<Integer>();
        //adding elements into the  primes collection using 
        //boolean add(E e)
        primes.add(2);
        primes.add(3);
        primes.add(5);
        primes.add(7);
        System.out.println("prime numbers are : " + primes);
        ArrayList<Integer> non_primes = new ArrayList<Integer>();
        //adding elements into the  non_primes collection using  
        //boolean add(E e)
        non_primes.add(4);
        non_primes.add(6);
        non_primes.add(8);
        System.out.println("non-prime numbers are : " + non_primes);

         ArrayList<Integer> numbers = new ArrayList<Integer>();  
         //adding elements into numbers collection using 
         //void add(int index, E element)
         numbers.add(0,1);
         //adding elements into numbers collection using 
         //booelan addAll(Collection<? extends T> c)
         numbers.addAll(primes);
         numbers.addAll(non_primes);
         System.out.println("Numbers are : " + numbers);


    }
}
/* 
Output: 
prime numbers are : [2, 3, 5, 7]
non-prime numbers are : [4, 6, 8]
Numbers are : [1, 2, 3, 5, 7, 4, 6, 8]
*/