Program to implement how to get and print size of an ArrayList in Java.
Output of the program :
package com.hubberspot.arraylist;
import java.util.ArrayList;
import java.util.List;
public class ArrayListSizeDemo {
public static void main(String[] args) {
// Create a ArrayList object which can store
// collection of String objects
List< String > stringList = new ArrayList< String >();
// Add few String objects to it
stringList.add("Neha");
stringList.add("Abhishek");
stringList.add("Deepali");
stringList.add("Snehal");
stringList.add("Vijay");
stringList.add("Aditya");
// In order to get number of objects stored in
// a list we can call size() method which will
// return an int value stating the size of ArrayList
int size = stringList.size();
// printing out the size of ArrayList on the console
System.out.println("Size of String ArrayList : " + size);
// Create a ArrayList object which can store
// collection of Integer objects
List< Integer > integerList = new ArrayList< Integer >();
// Add few Integer objects to it
integerList.add(1);
integerList.add(2);
integerList.add(3);
integerList.add(4);
integerList.add(5);
integerList.add(6);
// In order to get number of objects stored in
// a integer list we can call size() method which will
// return an int value stating the size of ArrayList
size = integerList.size();
// printing out the size of ArrayList on the console
System.out.println("Size of Integer ArrayList : " + size);
}
}
Output of the program :
