Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to get and print size of an ArrayList in Java ?.

Program to implement how to get and print size of an ArrayList in Java.

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 : 

 
 
© 2021 Learn Java by Examples Template by Hubberspot