
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the size of an ArrayList in Java
The size of an ArrayList can be obtained by using the java.util.ArrayList.size() method as it returns the number of elements in the ArrayList i.e. the size.
A program that demonstrates this is given as follows −
Example
import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) { List aList = new ArrayList(); aList.add("Apple"); aList.add("Mango"); aList.add("Guava"); aList.add("Orange"); aList.add("Peach"); System.out.println("The size of the ArrayList is: " + aList.size()); } }
Output
The size of the ArrayList is: 5
Now let us understand the above program.
The ArrayList aList is created. Then ArrayList.add() is used to add the elements to the ArrayList. ArrayList.size() returns the size of the ArrayList and that is displayed. A code snippet which demonstrates this is as follows −
List aList = new ArrayList(); aList.add("Apple"); aList.add("Mango"); aList.add("Guava"); aList.add("Orange"); aList.add("Peach"); System.out.println("The size of the ArrayList is: " + aList.size());
- Related Questions & Answers
- Get the location of an element in Java ArrayList
- Get the index of a particular element in an ArrayList in Java
- Get the size of the IdentityHashMap in Java
- Get the last index of a particular element in an ArrayList in Java
- Get the unmodifiable view of the specified ArrayList in Java
- Get Size of Java LinkedHashMap
- Get Size of Java LinkedHashSet
- Get size of HashSet in Java
- Get Size of TreeSet in Java
- Get the size of the LabelValue Tuple in Java
- Difference between length of Array and size of ArrayList in Java
- Search an element of ArrayList in Java
- What is the difference between the size of ArrayList and length of Array in Java?
- Java Program to Get the Size of the Collection
- Adding elements in the middle of an ArrayList in Java
Advertisements