The ArrayList is a data structure in Java that is considered dynamic in nature since it can be resized easily. When new elements are added/inserted into the array, the size of the given array increases. While the array’s size decreases when the elements are removed from it.
This article will demonstrate how to create ArrayLists in Java and perform particular operations on them.
How to Create/Generate an ArrayList in Java?
The ArrayList in Java is also called a resizable array since the array size increases and decreases dynamically. The simple/traditional arrays in Java are fixed-size arrays whose size can’t be changed once declared. To overcome this problem, the ArrayList is most commonly used in Java because of its dynamic nature. The ArrayList class must be imported from the util package to create/use the ArrayLists.
Let us go through a code example to understand the creation of ArrayList in Java.
Example: Creation of ArrayList in Java
The below code depicts the creation of an ArrayList using the add() method of Java:
//Import the required packages.
import java.util.*;
//Declare a new class
class arrayListExample {
//main method
public static void main(String[] args){
//Creating Dynamic Array
ArrayList<String>csfields = new ArrayList<>();
// Add new elements
csfields.add("Artificial Intelligence");
csfields.add("Data Science");
csfields.add("Networking");
csfields.add("System security");
//Print the results.
System.out.println("The Array list is : " + csfields);
}
}
In the above Java code:
- The required package to declare the ArrayList is imported.
- The main class contains the ArrayList declared as csfields.
- The csfileds implements the add() method which adds the fields in the ArrayList.
- The println() method prints the ArrayList on the screen.
Output
The output below shows the ArrayList that contains four elements:

Basic Operations With ArrayList in Java
When the ArrayList is created, a set of operations can be performed on that ArrayList as per the users’ needs. These operations include adding the elements, changing the elements, accessing the elements from the ArrayList, and deleting the elements from the ArrayList. Below is the code implementation of the basic operations of an ArrayList.
Operation 1: Add/Insert Elements to the ArrayList
The add() function of the ArrayList class adds/inserts the desired items/elements to the given ArrayList.
import java.util.*;
class arrayListExample {
public static void main(String[] args){
//Creating a new arraylist ArrayList<Integer>c = new ArrayList<>();
// adding new elements
c.add(1);
c.add(2);
c.add(3);
c.add(4);
//Print the results.
System.out.println("The Array list is : " + c);
}
}
In the above code:
- The java.util.ArrayList package is imported to declare the ArrayList.
- In the main class, the add() method is used to add integers in the ArrayList.
- The array is printed using the println() method.
Output
The output below shows the integer values added into the given ArrayList using the add() method and printed using the println() method.

Operation 2: Change Elements of the ArrayList
The set() method changes an element in an ArrayList. To do that, it accepts an index position along with the new element that is to be changed at the specified index position, consequently, it replaces the specified index with the given value:
//Import the required packages.
import java.util.ArrayList;
class arrayListExample {
public static void main(String[] args){
//Create a new arraylist named csfields
ArrayList<String>csfields = new ArrayList<>();
// insert new elements to ArrayList
csfields.add("Artificial Intelligence");
csfields.add("Data Science");
csfields.add("Networking");
csfields.add("System security");
System.out.println("The Array list is : " + csfields);
csfields.set(1,"Cyber Security");
//Print the results.
System.out.println("The Modified array list now is : " + csfields);
}
}
In the code above:
- The main class first uses the add() method to add the string values to the ArrayList declared as “csfields”.
- The set() method with the index position and the string to be replaced is invoked to change the value of the given ArrayList at index 1.
- The list containing the new element on the specified index position is printed using Java’s println() method.
Output
The below output consists of the original list and the modified one that contains the changed value on index 1.

Operation 3: Access/Get Elements from the ArrayList
The get() method is used to access/fetch the ArrayList’s elements. The code example below depicts the implementation of the get() method.
//Import the required packages.
import java.util.*;
class arrayListExample {
public static void main(String[] args){
//Create an arraylist named csfield ArrayList<String>csfields = new ArrayList<>();
// insert new elements to csfields
csfields.add("Artificial Intelligence");
csfields.add("Data Science");
csfields.add("Networking");
csfields.add("System security");
System.out.println("The Array list is : " + csfields);
//The get() method fetches the element required from the respective index.
String s=csfields.get(2);
//Print the results.
System.out.println("The element that is present on index 2 is: " + s);
}
}
In the above code, the get() method is used to fetch the required element by providing the index of that specific element.
Output
The output below represents the implementation of the get() method that prints the element at index number 2 as per requirement.

Operation 4: Remove an Element From the ArrayList
The remove() method is responsible for removing the specified element from the ArrayList.
//Import the required packages.
import java.util.*;
class arrayListExample {
public static void main(String[] args){
ArrayList<String>csfields = new ArrayList<>();
// Adding elements
csfields.add("Artificial Intelligence");
csfields.add("Data Science");
csfields.add("Networking");
csfields.add("System security");
System.out.println("The Array list is : " + csfields);
//The get() method fetches the element required from the respective index.
String s=csfields.remove(3);
//Print the results.
System.out.println("The List after removal is: " + csfields);
}
}
In the above code, the remove() method is used to eliminate the element present at index 3 in the given ArrayList.
Output
The output below shows that the remove() method has eliminated the element with index number 3 from the ArrayList.

This article sums up the implementation of ArrayList in Java and the basic operations performed on the ArrayList.
Conclusion
To create an ArrayList in Java, first import the ArrayList class from the util package, create an object of the ArrayList class, and use that object with the add() function to add elements to the newly created ArrayList. The ArrayList in Java is dynamic in nature and implements dynamic array functionality. It can dynamically resize itself when elements are added or removed from it, which makes it different from a fixed-size array. In this write-up, we have implemented the basic ArrayList operations using practical examples.