What is an Arraylist in java?
- In Java, an ArrayList may be a portion of the Java Collections Frameworks and belongs to the java.util package.
- It may be a energetic array-like data structure that permits components to be put away and gotten to in a successive way.
- Not at all like normal array in Java, which have a settled measure, an ArrayList can develop and shrink powerfully as components are added or deleted.
- This makes it one of the foremost commonly utilized data structures in Java for putting away and controlling information.

Key Highlights of ArrayList in Java
- Dynamic Sizing: Not at all like array, the size of an ArrayList can develop or shrink consequently when components are added or removed.
- Index-based Access: It permits random access to components utilizing an record, comparable to arrays.
- Resizing: Inside, an ArrayList employments an array to store components. When the size of the array is surpassed, the ArrayList resizes itself, by and large multiplying the size of the array.
- Null Components: ArrayList permits the size of invalid values, in spite of the fact that over the top utilize of invalid can lead to NullPointerExceptions in the event that not dealt with appropriately.
- Homogeneity: An ArrayList ordinarily stores components of the same sort (in spite of the fact that utilizing generics, you’ll be able make an ArrayList of any protest sort).
Types of ArrayList in Java
The Java ArrayList lesson itself does not have different “types” within the conventional sense, but it can be utilized in numerous settings based on the types of components it holds. The major varieties are:
Generics ArrayList:
- The foremost common type, where the ArrayList is utilized to store components of a particular type, indicated through Java Generics.
Example:
ArrayList names = modern ArrayList();
Here, the ArrayList is utilized to store String components.
Non-Generic ArrayList:
- Usually the older form some time recently generics were presented in Java 5.
- You’ll be able make an ArrayList without indicating the types of components it’ll hold, but this approach is debilitated since it sacrifices type security.
Example:
ArrayList list = modern ArrayList();
- In this case, the ArrayList can store any sort of protest.
ArrayList with Custom Objects:
- You’ll be able too make an ArrayList that holds occurrences of custom classes or objects.
Example:
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Arraylist<Person> people = new ArrayList<>();
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 30));
- Here, the ArrayList holds Individual objects.
Operations on Arraylist
There are several methods to manipulate the data it holds, such as:
1.add(E e):Adds a component to the end of the list.
ArrayList<Integer> numberList = new ArrayList<>();
numberList.add(10); // Adds 10 at the end
2.add(int list, E component):Includes a component at a indicated list.
numberList.add(1, 20); // Adds 20 at index 1
3.get(int record):Retrieves the component at the specified list.
System.out.println(numberList.get(0)); // Prints 10
4.remove(int index):Removes the component at the specified index.
numberList.remove(1); // Removes the element at index 1
5.size():Returns the number of elements inside the ArrayList.
System.out.println(numberList.size()); // Prints the size of the list
6.clear():Removes all components from the ArrayList.
numberList.clear(); // Clears the list
7.contains(Object o):Checks in case a particular component exists inside the list.
boolean hasTen = numberList.contains(10); // Checks if 10 is in the list
8.set(int index, E component):Replace the component at the specified index with a new one.
numberList.set(0, 50); // Replace the element at index 0 with 50
9.isEmpty():Returns true in case the ArrayList is empty.
System.out.println(numberList.isEmpty()); // Prints true or false
Illustration Code for ArrayList
Here’s a basic case that illustrates making an ArrayList, including components, and performing operations:
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList to store String values
ArrayList<String> fruits = new ArrayList<>();
// Add elements to the ArrayList
fruits.add(“Apple”);
fruits.add(“Banana”);
fruits.add(“Cherry”);
// Print the ArrayList
System.out.println(fruits); // [Apple, Banana, Cherry]
// Get an element by index
System.out.println(fruits.get(1)); // Banana
// Remove an element
fruits.remove(“Banana”);
System.out.println(fruits); // [Apple, Cherry]
// Check size of the ArrayList
System.out.println(fruits.size()); // 2
// Check if an element exists
System.out.println(fruits.contains(“Cherry”)); // true
// Replace an element
fruits.set(1, “Date”);
System.out.println(fruits); // [Apple, Date]
}
}
Performance Considerations
Whereas ArrayList is flexible, its execution can shift depending on the operation:
- Random Access: O(1) time complexity for getting to components by index.
- Adding at the End: O(1) amortized time complexity. Be that as it may, if resizing is required, it may take O(n).
- Adding/Removing at the Starting or Middle: O(n) time complexity due to moving of components.