Java ArrayList.size() Method
public int size()
The size() method is used to get the number of elements in an ArrayList object.
Package: java.util
Java Platform: Java SE 8
Syntax:
size()
Return Value:
The number of elements in this list
Return Value Type: int
Pictorial presentation of ArrayList.size() Method
Example: ArrayList.size() Method
The following example prints the size of an ArrayList object.
import java.util.*;
public class test {
public static void main(String[] args)
{
// Create an ArrayList of 4 colors
ArrayList<String> color_list = new ArrayList<String>(5);
color_list.add(new String("While"));
color_list.add(new String("Black"));
color_list.add(new String("Red"));
color_list.add(new String("Green"));
// Get the size of the ArrayList.
int size = color_list.size();
System.out.println("ArrayList contains " + size +
" elements.");
}
}
Output:
F:\java>javac test.java F:\java>javac test.java F:\java>java test ArrayList contains 4 elements.
Java Code Editor:
Previous:ensureCapacity Method
Next:isEmpty Method
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-tutorial/arraylist/arraylist_size.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics