ArrayList is an element of the Java collection framework and it is a class of java.util package. It gives us dynamic arrays in Java. Though, it may be more gradual than standard arrays but can be useful in programs where the excess of manipulation in the array is required. This class is found in java.util package. The main benefits of ArrayList are that if we display an array, then it is required to mention the size, but in ArrayList, it is not required to mention the size of ArrayList if you want to.
The Java ArrayList class keeps the elements in a dynamic array. It is like an array with no size limit. We can add or remove elements anytime. So it is much more adaptable than the traditional array. The ArrayList in Java can have duplicate features as well. It executes the List interface, so we can use all the techniques of the List interface here. The array list supports the insertion order internally. It inherits the AbstractList class and executes the List interface.
The ArrayList class is a resizable array and can be found in java.util package. The contrast between a built-in array and an ArrayList in Java is that the size of an array cannot be changed (if you want to add or remove elements to or from an array, you have to form a new one). while elements can be added to and removed from an ArrayList whenever you want.
Our Learners Also Read: What is Overriding in Java?
Here is a program to initialize ArrayList in Java using add() method
// Java code to illustrate initialization
// of ArrayList using add() method
import java.util.*;
public class GFG {
public static void main(String args[])
{
// create an ArrayList String type
ArrayList
// Initialize an ArrayList with add()
gfg.add("21st");
gfg.add("century");
gfg.add("girl");
// print ArrayList
System.out.print("ArrayList : " + gfg);
}
}
OUTPUT:
ArrayList : [21st, century, girl]
Here is a program to demonstrate the conversion of Array to ArrayList in Java of fixed size.
// Java program to demonstrate conversion of
// Array to ArrayList of fixed-size.
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Ram", "Ayush",
"Shivam", "Nandini"};
// Conversion of array to ArrayList
// using Arrays.asList
List al = Arrays.asList(geeks);
System.out.println(al);
}
}
OUTPUT:
[Ram, Ayush, Shivam, Nandini]
Here is a program to show how Objects Can an ArrayList Hold in Java
// Java Program to add elements to List
// Importing ArrayList class from
// java.util package
import java.util.ArrayList;
// Class
public class GFG {
// Mai driver method
public static void main(String[] args)
{
// Creating an ArrayList object
// (Declaring List of String type)
ArrayList
// Adding (appending) elements to List
// Custom inputs using add() method
names.add(“Computer”);
names.add(“Science”);
names.add(“Portal”);
// Printing all the elements of ArrayList
// Declaring generic ArrayList of String type
System.out.print(names);
}
}
OUTPUT:
[Computer, Science, Portal]
A dynamically sized collection of elements is stored by ArrayList in Java. Unlike arrays that are fixed in size, an ArrayList increases its size by itself when further elements are added to it. ArrayList is a component of Java’s collection framework and executes Java’s List interface.
About The Author:
Digital Marketing Course
₹ 29,499/-Included 18% GST
Buy Course₹ 41,299/-Included 18% GST
Buy Course