Home »
Java Programs »
Java ArrayList Programs
Java program to create an ArrayList, add elements and print
In this java program, we are going to learn how to create an ArrayList, add elements in the ArrayList and print the elements on the output screen?
By IncludeHelp Last updated : December 31, 2023
Problem statement
Here, we are creating an object of ArrayList and adding 5 elements (100, 200, 300, 400 and 500) in it.
Adding elements to an ArrayList
To add elements in the ArrayList, we use add() method, which is a method of ArrayList class.
Java program to add elements an ArrayList
import java.util.ArrayList;
public class ExArrayList {
public static void main(String[] args) {
////Creating object of ArrayList
ArrayList arrList = new ArrayList();
//adding data to the list
arrList.add("100");
arrList.add("200");
arrList.add("300");
arrList.add("400");
arrList.add("500");
System.out.println("Array List elements: ");
//display elements of ArrayList
for(int iLoop=0; iLoop < arrList.size(); iLoop++)
System.out.println(arrList.get(iLoop));
}
}
Output
The output of the above example is:
Array List elements:
100
200
300
400
500
Java ArrayList Programs »