Java ArrayList
The array is a collection of elements of the same data type, stored in a contiguous memory location. A list is a non-linear collection of data where the data stored can be of different types, and also, the data is stored in non-contiguous memory locations.
An array has a hardcoded limit, i.e., the size of an array is always predefined which can't be increased dynamically. A list, on the other hand, has a variable limit, i.e., the size of a list can be increased dynamically.
ArrayList in Java is the list of array. An ArrayList combines the properties of List and of an Array, i.e., in ArrayList, the size is varied dynamically at runtime which also indicates that instead of a stack, a heap is used.
Java is completely an object-oriented language, hence everything in Java is a class. And each class is stored in a given package. The ArrayList class is stored in java.util package.
The method to use and implement ArrayList Class has been demonstrated below.
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- }
- }
In the above code, we are creating an object of the ArrayList class. The above array list will store all the elements as String.
ArrayList Constructors
- Arraylist.ArrayList()
Used to build an empty ArrayList.
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList cars = new ArrayList();
- }
- }
- Arraylist.ArrayList<Collection c>()
This constructor is used to build an ArrayList initialized with the elements from the collection c.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- }
- }
- ArrayList(int capacity)
This constructor is used to build an ArrayList with initial capacity being specified.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- int capacity = 6;
- ArrayList < String > cars = new ArrayList < String > (capacity);
- }
- }
Features of ArrayList
- The ArrayList class has predefined some methods to access and modify ArrayList Elements.
- The size of an ArrayList can be increased or decreased whenever needed.
- ArrayList only supports object entries, not the primitive data types.
- In ArrayList, the actual objects are never stored at contiguous locations. References of the actual objects are stored at contiguous locations.
ArrayList Methods
- Arraylist.add()
Syntax
a. void add(int index, E element)
This version of the method inserts the specified element E at the specified position in this list. It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).
b. boolean add(E e)
This version of the method appends the specified element to the end of this list.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- }
- }
This code will add "Ford" as the first element of the String ArrayList, i.e. now the size of has increased to one which was initially zero. In the same way.
- Arraylist.addAll()
Syntax
a. boolean addAll(Collection c)
This version of AddAll() is used to append 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.
b. boolean addAll(int index, Collection c)
This version of AddAll() is used to append all the elements in the specified collection, starting at the specified position of the list.
- import java.util.ArrayList;
-
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
-
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
-
- ArrayList < String > new_cars = new ArrayList < String > ();
-
- new_cars.add("Bukati");
-
- new_cars.addAll(cars);
- }
- }
In the above program, we are creating two ArrayLists with name cars and new_cars respectively. And then we will copy all the elements of cars into new_cars.
- Arraylist.clear()
Syntax
void clear()
It is used to remove all of the elements from this list.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- cars.clear();
- }
- }
In the above code, the clear Method will delete all the inserted values for the ArrayList cars
- Arraylist.ensureCapacity()
Syntax
void ensureCapacity(int requiredCapacity)
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- cars.ensureCapacity(5000);
- }
- }
The above code will make sure that cars can hold up to 5000 elements.
- Arraylist.Get()
Syntax
E get(int index)
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- String c = cars.get(2);
- }
- }
The above code will fetch the third element i.e. "Maruti" from cars
- Arraylist.isEmpty()
Syntax
boolean isEmpty()
It returns true if the list is empty, otherwise false.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- ans = cars.isEmpty();
- if (ans == true)
- System.out.println("it is empty");
- else
- System.out.println("It is not empty");
- }
- }
The above code will check whether the ArrayList cars are empty or not. If it is empty then it will print "It is empty".
- Arraylist.LastIndexOf()
Syntax
int lastIndexOf(Object o)
It is used to return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- int element = arr.lastIndexOf("Maruti");
- if (element != -1)
- System.out.println("the lastIndexof of" + " Maruti is " + element);
- else
- System.out.println("Maruti is not present in" + " the list");
- }
- }
The above code will check the latest occurrence of the element "Maruti", and returns the integer index
- Arraylist.toArray()
Syntax
a. Object[] toArray()
b. <T> T[] toArray(T[] a)
It is used to return an array containing all of the elements in this list in the correct order.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- Object[] objects = cars.toArray();
- }
- }
The above code will convert the ArrayList object into an array of type Object
- Arraylist.clone()
Syntax
Object clone()
It is used to return a shallow copy of an ArrayList.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- ArrayList < String > new_cars = new ArrayList < String();
- new_cars = cars.clone();
- }
- }
A shallow copy means that when we are creating a copy we are not assigning memory to the new copy, instead of that we are using the new copy as a pointer to which we pass the address of the original ArrayList.
So in the above code, we are passing the address of cars to new_cars
- Arraylist.contains()
Syntax
boolean contains(Object o)
It returns true if the list contains the specified element
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- boolean ans = arr.contains("Ford");
- if (ans)
- System.out.println("The list contains Ford");
- else
- System.out.println("The list does not contain Ford");
- }
- }
In the above code, we are checking for the presence of "Ford". If we can find it in the ArrayList cars, we print "the list contains Ford" otherwise we print "The list does not contain Ford."
- Arraylist.indexOf()
Syntax
int indexOf(Object o)
It is used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- boolean ans = arr.contains("Ford");
- int element = arr.indexOf("Maruti");
- if (element != -1)
- System.out.println("the indexof of" + " Maruti is " + element);
- else
- System.out.println("Maruti is not present in" + " the list");
- }
- }
The above code will check the least recent occurrence of the element "Maruti", and returns the integer index
- Arraylist.remove()
Syntax
1. E remove(int index)
It is used to remove the element present at the specified position in the list.
2. boolean remove(Object o)
It is used to remove the first occurrence of the specified element.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- boolean ans = arr.contains("Ford");
- cars.remove(2);
- cars.remove("Ford");
- }
- }
In the above code, first, we delete "Maruti" and "Ford" from ArrayList cars
- Arraylist.removeAll()
Syntax
boolean removeAll(Collection<?> c)
It is used to remove all the elements from the list.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- boolean ans = arr.contains("Ford");
- cars.removeAll(cars);
- }
- }
In the above, we will remove the whole of the ArrayList cars
Note
clear() is faster than that of remove()
- Arraylist.removeIf()
boolean removeIf(Predicate<? super E> filter)
It is used to remove all the elements from the list that satisfies the given predicate
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- boolean ans = arr.contains("Ford");
- cars.removeIf(n -> (n == " Maruti"));
- }
- }
In the above code, we are removing all the occurrences of "Maruti" from the ArrayList cars.
- Arraylist.removeRange()
Syntax
protected void removeRange(int fromIndex, int toIndex)
It is used to remove all the elements lies within the given range.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- boolean ans = arr.contains("Ford");
- cars.removeRange(0, 2)
- }
- }
In the above code, we are removing the first 2 elements i.e "Ford" and "Ferrari"
- Arraylist.replaceAll()
Syntax
void replaceAll(UnaryOperator<E> operator)
It is used to replace all the elements from the list with the specified element.
- import java.util.ArrayList;
- import java.util.function.*;
- class MyOperator < T > implements UnaryOperator < T > {
- T varc1;public T apply(T varc) {
- return varc1;
- }
- }
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- MyOperator < String > operator;
- operator = new MyOperator < > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- operator.varc1 = "Ambassodor";
- cars.replaceAll(operator);
- }
- }
In the above code, we replace "Ford", "Ferarri", "Maruti" and "Volkswagen" with "Ambassador" i.e. the whole of the ArrayList cars will contain only "Ambassador"
- Arraylist.retainAll()
Syntax
void retainAll(Collection<?> c)
It is used to retain all the elements in the list that are present in the specified collection.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > (capacity);
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- ArrayList < String > new_cars = new ArrayList < String > ();
- new_cars.add("Ford");
- new_cars.add("Ferari");
- new_cars.add("Mecedes");
- new_cars.add("Benz");
- new_cars.retainAll(cars);
- }
- }
In the above code, we will be removing "Mercedes" and "Benz" from ArrayList new_cars as these two values are not present in ArrayList cars.
- Arraylist.set()
Syntax
E set(int index, E element)
It is used to replace the specified element in the list, present at the specified position.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- cars.set(, "Benz");
- }
- }
In the above code, we are replacing the 3rd element values with "Benz", i.e., "Maruti" will be replaced with "Benz".
- Arraylist.sort()
Syntax
void sort(Comparator<? super E> c)
It is used to sort the elements of the list based on the specified comparator.
- import java.util.ArrayList;
- import java.util.Collections.sort()
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- Collections.sort(cars);
- }
- }
In above the code, we will be sorting the above list according to the ASCII value as the ArrayList is of String Collection type, i.e. "Ferrari", "Ford", "Maruti", "Volkswagen"
- Arraylist.spliterator()
Syntax
Spliterator<E> spliterator()
It is used to create spliterator over the elements in a list.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- Spliterator < String > new_cars = cars.spliterator();
- }
- }
In the above code, we have created a Spliterator named new_cars from the ArrayList cars.
- Arraylist.subList()
Syntax
List<E> subList(int fromIndex, int toIndex)
It is used to fetch all the elements lies within the given range.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- Spliterator < String > new_cars = cars.spliterator();
- }
- }
In the above code, we are creating an ArrayList new_cars which contains the values "Ferrari" and "Maruti".
- Arraylist.size()
Syntax
int size()
It is used to return the number of elements present in the list.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- ArrayList < String > cars = new ArrayList < String > ();
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- int size = cars.size();
- }
- }
In the above code, we are finding the number of elements that are present in ArrayList cars
- Arraylist.trimToSize()
Syntax
void trimToSize()
It is used to trim the capacity of this ArrayList instance to be the list's current size.
- import java.util.ArrayList;
- public class Csharpcorner {
- public static void main(String[] args) {
- int capacity = 20;
- ArrayList < String > cars = new ArrayList < String > (capacity);
- cars.add("Ford");
- cars.add("Ferrari");
- cars.add("Maruti");
- cars.add("Vokesvagen");
- cars.trimToSize();
- }
- }
In the above code, we will be reducing the size of ArrayList cars from 20 to 4 (i.e., current ArrayList size)
Conclusion
In this article, we learned about ArrayList, the difference between Array and List, features of ArrayList, ArrayList Methods, and how to use ArrayList Class and Methods using Java.