
How can we use array list in java program ?

import java.util.ArrayList;
public class ArrayListExample {
public static void main(String [] args){
ArrayList<String> array = new ArrayList<String>();
array.add("Amit");
array.add("Ankit");
array.add("Vinay");
array.add("Deepak");
array.add("Naulej");
array.add("Arun");
System.out.println("ArrayList Size:"+array.size());
for(int i=0;i<array.size();i++){
System.out.println("ArrayList Element"+i+" :"+array.get(i));
}
}
}
Output
ArrayList Size:6 ArrayList Element0 :Amit ArrayList Element1 :Ankit ArrayList Element2 :Vinay ArrayList Element3 :Deepak ArrayList Element4 :Naulej ArrayList Element5 :Arun
Description:- The above program demonstrates you the use of ArrayList. An ArrayList is a data structure. It can be dynamically resized. Here, we have created an array list and store string values to it. The method size() of ArrayList class returns the size of the arraylist and get() method fetch the string values from the arraylist.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.