ArrayList is a part of the Collection framework and is located in java.util package. It is a dynamic array that provides the ability to increase its size during runtime. Although it may be slower than standard arrays, it can be useful in programs that require a lot of manipulation in the array.
There are several advantages of using ArrayList over Arrays. Firstly, its dynamic size allows for an increase in the number of elements stored in the ArrayList without the need to specify a predefined size or number of elements. This feature is particularly useful in situations where the number of elements to be stored is unknown beforehand.
Secondly, ArrayList provides a rich library of built-in functions that perform complex operations efficiently. For instance, the add() function can add an element directly to the middle of the ArrayList, while a function exists to find the first occurrence of an item, as well as one to remove an item.
It is worth noting that since ArrayList is part of the Collections framework, it can only store data of non-primitive types.
Below is a sample program that demonstrates the use of ArrayList in Java:
javaimport java.util.*class GFG{public static void main(String[]args){// Create an ArrayList
ArrayList<Integer> al = new ArrayList<Integer>();
// Add elements to the ArrayList
al.add(10);
al.add(20);
al.add(30);
// Print the ArrayList
System.out.println(al);
}}
The output of the above program will be [10, 20, 30].
It is also possible to specify the size of an ArrayList when creating it. This allows the ArrayList to be used as a normal array.