C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

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?
Submitted by IncludeHelp, on October 19, 2017

Here, we are creating an object of ArrayList and adding 5 elements (100, 200, 300, 400 and 500) in it.

To add elements in the ArrayList, we use "add()" method, which is a method of ArrayList class.

Consider the program

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

Array List elements: 
100
200
300
400
500






COMMENTS