Approach – Using size() Method
The size() method of the ArrayList class returns the number of elements present in the list. This method is part of the java.util package and is very straightforward to use. It helps in getting the current number of elements stored in the ArrayList.
Syntax
The syntax for the size() method is:
int size()
This method returns an integer value representing the number of elements in the ArrayList.
Example 1 – Java Program to Determine the Size of an Integer ArrayList
Here’s a simple Java program to find the size of an Integer ArrayList:
Java
import java.util.ArrayList;
public class IntegerArrayListSize {
public static void main(String[] args) {
// Creating an ArrayList of Integer type
ArrayList<Integer> intList = new ArrayList<>();
// Adding elements to the ArrayList
intList.add(10);
intList.add(20);
intList.add(30);
intList.add(40);
intList.add(50);
// Determining the size of the ArrayList
int size = intList.size();
// Printing the size of the ArrayList
System.out.println("The size of the Integer ArrayList is: " + size);
}
}

You can also try this code with Online Java Compiler
Run Code
Explanation
- We import the ArrayList class from the java.util package.
- We create an ArrayList of Integer type named intList.
- We add five integer elements to intList.
- We use the size() method to determine the number of elements in the ArrayList.
- We print the size of the ArrayList.
Output
The size of the Integer ArrayList is: 5
Example 2 – Java Program to Determine the Size of a String ArrayList
Here’s a simple Java program to find the size of a String ArrayList:
Java
import java.util.ArrayList;
public class StringArrayListSize {
public static void main(String[] args) {
// Creating an ArrayList of String type
ArrayList<String> strList = new ArrayList<>();
// Adding elements to the ArrayList
strList.add("Apple");
strList.add("Banana");
strList.add("Cherry");
strList.add("Date");
strList.add("Elderberry");
// Determining the size of the ArrayList
int size = strList.size();
// Printing the size of the ArrayList
System.out.println("The size of the String ArrayList is: " + size);
}
}

You can also try this code with Online Java Compiler
Run Code
Explanation
- We import the ArrayList class from the java.util package.
- We create an ArrayList of String type named strList.
- We add five string elements to strList.
- We use the size() method to determine the number of elements in the ArrayList.
- We print the size of the ArrayList.
Output
The size of the String ArrayList is: 5
Key Features
- Dynamic Sizing: The ArrayList can grow and shrink dynamically as elements are added or removed.
- Ease of Use: The size() method provides an easy way to determine the number of elements in the list.
- Type Safety: ArrayLists can be type-specific, ensuring that only objects of a specified type are stored.
Frequently Asked Questions
What is the size() method in Java?
The size() method is a part of the ArrayList class in Java. It returns the number of elements currently stored in the ArrayList.
Can the size() method be used with other collections in Java?
Yes, the size() method is available in other collection classes like HashSet, LinkedList, etc., to determine the number of elements they contain.
What will the size() method return if the ArrayList is empty?
If the ArrayList is empty, the size() method will return 0.
How can I remove all elements from an ArrayList?
You can use the clear() method to remove all elements from an ArrayList. For example:
intList.clear();
Conclusion
Finding the length or size of an ArrayList in Java is a simple task using the size() method. This method is efficient and straightforward, providing a quick way to determine the number of elements in the list. By understanding how to use this method, you can effectively manage and manipulate ArrayLists in your Java programs.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.