ArrayList length Java
Ngày đăng:
07/01/2022
Trả lời:
0
Lượt xem:
184
Oct 19, 2021by Sai gowtham
How to get length of an ArrayList in Javajava1min read 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() methodTo 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
|