Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Program to Find the Length/Size of an ArrayList
Next article icon

Java Program to Find the Length/Size of an ArrayList

Last Updated : 28 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an ArrayList in Java, the task is to write a Java program to find the length or size of the ArrayList.

Examples: 

Input: ArrayList: [1, 2, 3, 4, 5]  Output: 5    Input: ArrayList: [geeks, for, geeks]  Output: 3

ArrayList - An ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java. However, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.

Approach - Using size() method

The size of the ArrayList can be determined easily with the help of the size() method. This method does not take any parameters and returns an integer value which is the size of the ArrayList.

Syntax: 

int size = ArrayList.size();

Below is the implementation of the above approach:

Example 1 - Java Program to determine the size of an Integer ArrayList

Java
// Java program to find the size // of an ArrayList  import java.util.*;  public class GFG {     public static void main(String[] args)         throws Exception     {          // Creating object of ArrayList<Integer>         ArrayList<Integer>             arrlist = new ArrayList<Integer>();          // Populating arrlist         arrlist.add(1);         arrlist.add(2);         arrlist.add(3);         arrlist.add(4);         arrlist.add(5);          // print arrlist         System.out.println("ArrayList: "                            + arrlist);          // getting total size of arrlist         // using size() method         int size = arrlist.size();          // print the size of arrlist         System.out.println("Size of ArrayList = "                            + size);     } } 

Output
ArrayList: [1, 2, 3, 4, 5]  Size of ArrayList = 5

Example 2 - Java Program to determine the size of a String ArrayList

Java
// Java program to find the size // of an String ArrayList  import java.util.*;  public class GFG {     public static void main(String[] args)         throws Exception     {          // Creating object of ArrayList<Integer>         ArrayList<String>             arrlist = new ArrayList<String>();          // Populating arrlist         arrlist.add("GeeksforGeeks");         arrlist.add("a");           arrlist.add("computer");         arrlist.add("science");           arrlist.add("portal");         arrlist.add("for");           arrlist.add("geeks");          // print arrlist         System.out.println("ArrayList: "                            + arrlist);          // getting total size of arrlist         // using size() method         int size = arrlist.size();          // print the size of arrlist         System.out.println("Size of ArrayList = "                            + size);     } } 

Output
ArrayList: [GeeksforGeeks, a, computer, science, portal, for, geeks]  Size of ArrayList = 7

Next Article
Java Program to Find the Length/Size of an ArrayList

R

RishabhPrabhu
Improve
Article Tags :
  • Java
Practice Tags :
  • Java

Similar Reads

    How to Find Length or Size of an Array in Java?
    Finding the length of an array is a very common and basic task in Java programming. Knowing the size of an array is essential so that we can perform certain operations. In this article, we will discuss multiple ways to find the length or size of an array in Java.In this article, we will learn:How to
    3 min read
    Difference between length of Array and size of ArrayList in Java
    Array and ArrayList are two different Entities in Java. In this article we will learn the difference between length of Array and size of ArrayList in Java.Array has length property which provides the length of the Array or Array object. It is the total space allocated in memory during the initializa
    2 min read
    ArrayList trimToSize() Method in Java with Example
    In Java, the trimToSize() method is used to reduce the capacity of an ArrayList to match its current size. This helps to optimize memory usage when an ArrayList contains less elements than its allocated capacity.Example 1: Here, we will use the trimToSize() method to trim an ArrayList of Strings.Jav
    2 min read
    ArrayList size() Method in Java with Examples
    In Java, the size() method is used to get the number of elements in an ArrayList.Example 1: Here, we will use the size() method to get the number of elements in an ArrayList of integers.Java// Java program to demonstrate the use of // size() method for Integer ArrayList import java.util.ArrayList; p
    2 min read
    CopyOnWriteArrayList size() method in Java
    The size() method of CopyOnWriteArrayList returns the size of the list. It returns the number of elements in the list. Syntax: public int size() Parameters: The function does not accepts any parameter. Return Value: The function returns the size of the list. Below programs illustrate the above funct
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences