How to get length of an ArrayList in Java
In this tutorial, we are going to learn about how to get the length/size of a given ArrayList in Java.
Consider, we have the following ArrayList:
ArrayList<String> users = new ArrayList<>(Arrays.asList("john", "kevin", "rose"));Now, we need to find out the length of an above users arraylist.
Using size() method
To get the length of an ArrayList, we can use the built-in size() method of java.util.ArrayList class.
The size() method returns the total number of elements in a arraylist in integer format.
Here is an example, that gets the length of a users list:
import java.util.ArrayList;
import java.util.Arrays;
public class Main
{
public static void main(String[] args) {
ArrayList<String> users = new ArrayList<>(Arrays.asList("john", "kevin", "rose"));
int length = users.size();
System.out.println(length);
}
}Output:
3

