I am Utpal Vishwas from Uttar Pradesh. Have completed my B. Tech. course from MNNIT campus Prayagraj in 2022. I have good knowledge of computer networking.
There are two ways to create an array list in Java:
Using the ArrayList() constructor. The
ArrayList() constructor creates an empty array list.
ArrayList<String> arrayList = new ArrayList<>();
Using the add() method. The
add() method adds an element to the end of the array list.
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Hello");
Here is an example of how to create an array list and add some elements to it:
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
// Create an empty array list
ArrayList<String> arrayList = new ArrayList<>();
// Add some elements to the array list
arrayList.add("Hello");
arrayList.add("World");
// Print the array list
System.out.println(arrayList);
}
}
Output:
[Hello, World]
Here are some important things to note about creating array lists:
Array lists are dynamic data structures, which means that they can grow or shrink as needed.
Array lists are not synchronized, which means that they are not thread-safe.
Array lists are implemented using a linked list, which means that they are not as efficient as arrays for storing large amounts of data.
Liked By
Write Answer
How to create ArrayList in Java?
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
22-Jul-2023There are two ways to create an array list in Java:
ArrayList()
constructor. TheArrayList()
constructor creates an empty array list.add()
method. Theadd()
method adds an element to the end of the array list.Here is an example of how to create an array list and add some elements to it:
Output:
Here are some important things to note about creating array lists: