ArrayList in Java

Java ArrayList

ArrayList in Java

Class ArrayList is a part of Java Collection Framework which extends AbstractClass and implements List Interface. Class ArrayList is a resizable array and it is found in the java.util package. Class ArrayList is a generic class and its uses as Class ArrayList <E>.

What is the difference between array and ArrayList in java?


The array is a fixed size and it cannot be modified but ArrayList is dynamic in nature, it can grow and shrink.
Java Array is not a generic type whereas ArrayList is a generic type (Generic data types works with “any” data type). Java ArrayList class is none synchronized.
Java array can contain be both primitives data type and objects but ArrayList can contain only object elements.
By assignment operator data store in array but in ArrayList data entry with the help of add() method.

Java ArrayList Constructors

public ArrayList(): Most widely used Java ArrayList constructor. This ArrayList constructor will return ArrayList() Constructs an empty list.
ArrayList(Collection<? extends E> c) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.


ArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity. The capacity is the size of the underlying array.



Adding/Inserting String elements in ArrayList

import java.util.ArrayList;
public class School {
  public static void main(String[] args) {
    ArrayList<String> student = new ArrayList<String>();
    student.add("Amit");
    student.add("Sunil");
    student.add("Akbar");
    student.add("Shakil");
    student.add("Shakil");
    student.add("Shakil");
    System.out.println(student);
  }
}
Output:
[Amit, Sunil, Akbar, Shakil, Shakil, Kapil]

Adding/Inserting Integer  elements in ArrayList

import java.util.*;

public class IntegerArrayList {

    public static void main(String []args)
    {
        //Declaring ArrayList in integer
        ArrayList<Integer> intArrayList = new ArrayList<Integer>();
        intArrayList.add (5);
        intArrayList.add (2);
        intArrayList.add (7);
        intArrayList.add (8);
        intArrayList.add (9);
        //Displaying array list
        for (int i : intArrayList) {
            System.out.println("The ArrayList item: " + i);
        }
    }          
}
Output
The ArrayList item: 5
The ArrayList item: 2
The ArrayList item: 7
The ArrayList item: 8
The ArrayList item: 9

Change the elements of specified position

To change or modify an element, we use the set () method and refer to the index number:
import java.util.ArrayList;
public class School {
  public static void main(String[] args) {
    ArrayList<String> student = new ArrayList<String>();
    student.add("Amit");
    student.add("Sunil");
    student.add("Akbar");
    student.add("Shakil");
    student.add(1,"Uma");
    student.add("Kapil");
    System.out.println(student);
  }
}

Output
[Amit, Uma, Sunil, Akbar, Shakil, Kapil]

How to remove elements from the ArrayList?

Import java.util.ArrayList;
public class School {
  public static void main(String[] args) {
    ArrayList<String> student = new ArrayList<String>();
    student.add(“Amit”);
    student.add(“Sunil”);
    student.add(“Akbar”);
    student.remove(“Shakil”);
    student.remove(1);
    student.add(“Uma”);
    student.add(“Kapil”);
    System.out.println(student);
  }
}
Output:
[Amit, Akbar, Uma, Kapil]
Here element “Shakil” and “Sunil” have been deleted.

How to find the size of an ArrayList?

Using size () method we can find the size of an ArrayList.
import java.util.ArrayList;

public class School {
  public static void main(String[] args) {
    ArrayList<String> student = new ArrayList<String>();
    student.add("Amit");
    student.add("Sunil");
    student.add("Akbar");
    student.remove("Shakil");
    student.add("Uma");
    student.add("Kapil");
    System.out.println(student.size());
  }
}
Output:
5

How to display ArrayList elements through the loop?

With for Loop
import java.util.ArrayList;

public class School {
    public static void main(String[] args)
    {
        int i;
        ArrayList<String> student = new ArrayList<String>();
        student.add("Amit");
        student.add("Sunil");
        student.add("Akbar");
        student.remove("Shakil");
        student.add("Uma");
        student.add("Kapil");
        // Elements display through for loop
        for (i = 0; i < student.size(); i++) {
            System.out.println(student.get(i));
        }
    }
}
Output:
Amit
Sunil
Akbar
Uma
Kapil

With for each Loop
import java.util.ArrayList;

public class School {
    public static void main(String[] args)
    {
        ArrayList<String> student = new ArrayList<String>();
        student.add("Amit");
        student.add("Sunil");
        student.add("Akbar");
        student.remove("Shakil");
        student.add("Uma");
        student.add("Kapil");
        // Elements display through for each loop
        for (String i : student) {
            System.out.println(i);
        }
    }
}

How to sort an ArrayList element?

ArrayList elements can be sort with another sort() method which is in Collection class of java.util package.
import java.util.ArrayList;
import java.util.Collections; 
public class School {
    public static void main(String[] args)
    {
        ArrayList<String> student = new ArrayList<String>();
        student.add("Amit");
        student.add("Sunil");
        student.add("Akbar");
        student.remove("Shakil");
        student.add("Uma");
        student.add("Kapil");
        // Elements display in sort order
        Collections.sort(student);  //
        for (String i : student) {
            System.out.println(i);
        }
    }
}
Output
Akbar
Amit
Kapil
Sunil
Uma

Search in ArrayList java

We can search in ArrayList element by contains method. When we search an element in ArrayList, it will return true if found and return false if not found.
import java.util.*;

public class ArrayListSearch {

    public static void main(String []args)
    {
        Scanner sc = new Scanner(System.in);
        //Declaring ArrayList
        ArrayList<String> student = new ArrayList<String>();
        student.add ("Shami");
        student.add ("Virat");
        student.add ("Dhoni");
        student.add ("Vijay");
        student.add ("Amir");
        student.add ("Akbar");
        student.add ("Ajay");
        System.out.println(student);
        System.out.println("Enter a name to search: ");
        String searchName = sc.nextLine();
        if (student.contains(searchName)){
            System.out.println("The name is found!");
        }
        else
        {
            System.out.println("The name does not exist!");
        }
    }          
}
Output:
[Shami, Virat, Dhoni, Vijay, Amir, Akbar, Ajay]
Enter a name to search:
Dhoni
The name is found!

[Shami, Virat, Dhoni, Vijay, Amir, Akbar, Ajay]
Enter a name to search:
Nikhil
The name does not exist!

How to merge two ArrayLists in Java?

With the help of addAll() method we can add or merge two ArrayList into one.
import java.util.ArrayList;
import java.util.Arrays;

public class MergeArrayList
{
    public static void main(String[] args) throws Exception
    {
        // First ArrayList       
        ArrayList<String> student1 = new ArrayList<String>();
        student1.add ("Shami");
        student1.add ("Virat");
        student1.add ("Dhoni");
        student1.add ("Vijay");
        student1.add ("Amir");
        student1.add ("Ajay");

        System.out.println ("First ArrayList");
        System.out.println (student1);

        // Second ArrayList
        ArrayList<String> student2 = new ArrayList<String>();
        student2.add ("Amit");
        student2.add ("Sunil");
        student2.add ("Akbar");
        student2.add("Uma");
        student2.add("Kapil");

        System.out.println("Second ArrayList");
        System.out.println(student2);
       
         // Merge both lists
        student1.addAll(student2);
        // After Merging of two array
        System.out.println("After Merging of two array");
        System.out.println(student1);
    }
}
Output:
First ArrayList
[Shami, Virat, Dhoni, Vijay, Amir, Ajay]
Second ArrayList
[Amit, Sunil, Akbar, Uma, Kapil]
After Merging of two array
[Shami, Virat, Dhoni, Vijay, Amir, Ajay, Amit, Sunil, Akbar, Uma, Kapil]

Merge two ArrayList without duplicates

With the help of LinkedHashSet, we can merge two ArrayList without duplicate values. It will also intake the order.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class MergeArrayListNoDuplicate
{
    public static void main(String[] args) throws Exception
    {
        ArrayList<Integer> arrayList1 = new ArrayList<>(Arrays.asList(1, 3, 4, 5, 7, 8));

        ArrayList<Integer> arrayList2 = new ArrayList<>(Arrays.asList(11, 22, 4, 33, 55));

        System.out.println("First ArrayList :" + arrayList1);
        System.out.println("Second ArrayList:" + arrayList2);

        Set<Integer> hashSet = new LinkedHashSet<>(arrayList1);
        hashSet.addAll(arrayList2);
        List<Integer> mergeList = new ArrayList<>(hashSet);

        System.out.println("Merge ArrayList :" + mergeList);
    }
}
Output:
First ArrayList :[1, 3, 4, 5, 7, 8]
Second ArrayList:[11, 22, 4, 33, 55]
Merge ArrayList :[1, 3, 4, 5, 7, 8, 11, 22, 33, 55]

How do you convert an ArrayList to Array?

Converting ArrayList to the array we can use toArray() method, and it will return an array with all elements of ArrayList.

import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListToArray
{
   public static void main(String[] args) throws Exception
    {
        ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(1, 3, 4, 5, 7, 8));
        Object[] array = arrayList.toArray();

        //Printing the elements of the array.

        for (Object ob : array)
        {
            System.out.print(ob + " ");
        }
    }
}
Output:
1 3 4 5 7 8

We will also like following Post
Array in Java

Java Program for Binary Search



SHARE THIS
Previous Post
Next Post