How to count number of element in ArrayList

Size() Method return number of element in the arraylist.

import java.util.ArrayList;

public class CountElement {

public static void main(String[] args) {

ArrayList<String> arr = new ArrayList<String>();

// add element in arraylist

arr.add(“c”);
arr.add(“php”);
arr.add(“html”);
arr.add(“java”);
arr.add(“delhi”);
arr.add(“INDIA”);

// print arraylist

System.out.println(“arrayList is = ” + arr);

/*
* Returns the number of elements in this list.
*/

System.out.println(” size = “+arr.size());

}

}

Output:

arrayList is = [c, php, html, java, delhi, INDIA]

size = 6

 

Related Posts: 

 

Storing Java Object In ArrayList

 

Leave a Reply

Your email address will not be published. Required fields are marked *

78 − = 77