Java ArrayList.size() Method

 Posted On  | Yashwant Chavan 

In this tutorial you will learn about ArrayList.size() method. It is used to get the number of elements in this list.

syntax

Returns the number of elements in this list.

int java.util.ArrayList.size()

Return

return int value, which indicates number of elements in this list.

Java ArrayList.size() example

Refer below steps

  • Constructs an empty ArrayList with an initial capacity using new ArrayList<String>().
  • Add some elements in colors ArrayList using add() method.
  • Print the size of ArrayList
package com.technicalkeeda.app;

import java.util.ArrayList;

public class ArrayListSize {
    public static void main(String[] args) {

        ArrayList <String> colors = new ArrayList <String> ();
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");
        System.out.println("ArrayList Size:- " + colors.size());
        colors.add("Pink");
        colors.add("Black");
        System.out.println("Add more elements in ArrayList :- " + colors.size());

    }
}

Output

ArrayList Size:- 3
Add more elements in ArrayList :- 5


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0337 seconds.