BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to find length of ArrayList in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

You can find the length (or size) of an ArrayList in Java using size() method. The size() method returns the number of elements present in the ArrayList.

Syntax of size() method:

public int size()

Program to find length of ArrayList using size()

In this program, we are demonstrating the use of size() method. As you can see, when arraylist is created, the size of it is zero. After adding few elements to the arraylist, the size of arraylist changed to 5 as we have done 5 additions using add() method. We then removed two elements from the arraylist using remove() method and printed the size again, which came as 3. This shows that the size() method returns the number of elements in a arraylist present at that particular moment of time.

import java.util.ArrayList;
public class Details
{
  public static void main(String [] args)
  {
     ArrayList<Integer> al=new ArrayList<Integer>();
     System.out.println("Initial size: "+al.size());
     al.add(1);
     al.add(13);
     al.add(45);
     al.add(44);
     al.add(99);
     System.out.println("Size after few additions: "+al.size());
     al.remove(1);
     al.remove(2);
     System.out.println("Size after remove operations: "+al.size());
     System.out.println("Final ArrayList: ");
     for(int num: al){
        System.out.println(num);
     }
  }
}

Output:

Initial size: 0
Size after few additions: 5
Size after remove operations: 3
Final ArrayList: 
1
45
99

Another Example:
This is another example, where we have an arraylist of string type and we are using the size() method to find the length of arraylist after various add() and remove() operations.

import java.util.ArrayList;
public class JavaExample
{
  public static void main(String [] args)
  {
    ArrayList<String> cityList=new ArrayList<>();
    //adding elements to the arraylist
    cityList.add("Delhi");
    cityList.add("Jaipur");
    cityList.add("Agra");
    cityList.add("Chennai");

    //displaying current arraylist and size
    System.out.println("ArrayList: "+cityList);
    System.out.println("ArrayList size: "+cityList.size());

    //removing an element from arraylist
    cityList.remove("Delhi");

    //print arraylist and size
    System.out.println("ArrayList: "+cityList);
    System.out.println("ArrayList size: "+cityList.size());
  }
}

Output:

ArrayList: [Delhi, Jaipur, Agra, Chennai]
ArrayList size: 4
ArrayList: [Jaipur, Agra, Chennai]
ArrayList size: 3

Recommended articles:

  • Sort an ArrayList
  • ArrayList vs LinkedList
  • Convert ArrayList to Array
❮ Java ArrayListJava Collections ❯

Top Related Articles:

  1. Convert ArrayList to Array in Java
  2. ArrayList in Java With Examples
  3. How to Convert an array to ArrayList in java
  4. Difference between length of Array and size of ArrayList in Java
  5. How to get the last element of Arraylist?

Tags: Collections, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Vaibhav Chintawar says

    June 22, 2016 at 3:34 PM

    after removing al.remove(2); still why we are getting an output which is at 3rd position al.add(45)

    Reply
    • venkatesh says

      August 6, 2016 at 12:05 PM

      Because when we remove element at first index, the element 45 moves to index 1 and 44 moves to index 2.Now when we remove(2) 44 will be deleted.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap