- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- Get the location of an element in Java ArrayList
- Get the index of a particular element in an ArrayList in Java
- How to get Sublist of an ArrayList using Java?
- Get the last index of a particular element in an ArrayList in Java
- Get the unmodifiable view of the specified ArrayList in Java
- Difference between length of Array and size of ArrayList in Java
- Get the size of the IdentityHashMap in Java
- Get size of HashSet in Java
- Get Size of TreeSet in Java
- How to get ArrayList to ArrayList and vice versa in java?
- Get Synchronized List from ArrayList in java
- Search an element of ArrayList in Java
- Get the size of the LabelValue Tuple in Java
- Clear an ArrayList in Java
- Clone an ArrayList in Java

Advertisements