Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList size() Method

Posted on July 24, 2025July 24, 2025 By Admin No Comments on Java ArrayList size() Method

The size() method is a fundamental operation in the Java ArrayList class. It allows you to determine the number of elements currently stored in an ArrayList, making it a crucial tool for iteration, validation, and working with dynamic data collections.

What Is the size() Method in ArrayList?

  • The size() method returns the number of elements (items) present in the ArrayList.
  • It does not count the internal capacity—only the actual, current number of list elements.
  • The method is efficient, operating in constant time O(1) regardless of list size.

Syntax

public int size()

Parameters

  • The size() method does not take any parameters.

Return Value

ReturnsDescription
intThe number of elements in the list

Exceptions

  • The method does not throw any exceptions for standard ArrayList usage.
  • Always supported in standard implementations.

How Does size() Work Internally?

  • Internally, ArrayList maintains a count of added and removed elements.
  • size() simply returns this count—no element traversal or calculation each time.
  • Calling size() is very fast and safe to use even in loops or intensive operations.

Examples of the size() Method

1. Getting List Size

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        System.out.println("Number of names: " + names.size()); // Output: 3
    }
}

2. Use size() in a for Loop

import java.util.ArrayList;

public class Demo {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(5);
        numbers.add(10);
        numbers.add(15);

        for (int i = 0; i < numbers.size(); i++) {
            System.out.println("Element at index " + i + ": " + numbers.get(i));
        }
    }
}

Output:

Element at index 0: 5
Element at index 1: 10
Element at index 2: 15

3. Check If an ArrayList Is Empty Using size() method

import java.util.ArrayList;

public class Example {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();

        if (list.size() == 0) {
            System.out.println("The list is empty.");
        }
    }
}

(Prefer isEmpty() for clarity, but this approach is also valid.)

4. Size After Adding and Removing Elements

import java.util.ArrayList;

public class SizeTest {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        System.out.println("Before removal: " + fruits.size()); // Output: 2

        fruits.remove("Banana");
        System.out.println("After removal: " + fruits.size()); // Output: 1
    }
}

Important Notes

  • The size() method reflects current list contents only—not initial capacity or maximum size.
  • Adding or removing elements dynamically updates the value returned by size().
  • Useful for validating index ranges: valid indexes are from 0 to size()-1.
  • Use size() in loop boundaries and input validation.
  • For empty lists, size() returns 0.

Summary

AspectDetails
Method PurposeGet the current number of elements
Syntaxlist.size()
Return Valueint (number of elements)
ParametersNone
Throws ExceptionNo (in standard usage)
Typical Use CasesLooping, validation, data structure checks

Typical Use Cases

  • Looping over all elements: Safely bounded for-loops and index-based traversals.
  • Empty check: Combined with == 0 or in validation.
  • Dynamic collections: Monitor and react to changes in list content.
  • User interface updates: Displaying counts, inventory, or summary statistics.
Java Tags:Java-ArrayList, Java-ArrayList-Method, Java-Collections

Post navigation

Previous Post: Java ArrayList set() Method
Next Post: Java ArrayList sort() Method

Leave a Reply Cancel reply

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

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList remove() Method
  • Java ArrayList removeAll() Method
  • Java ArrayList removeIf() Method
  • Java ArrayList replaceAll() Method
  • Java ArrayList retainAll() Method
  • Java ArrayList set() Method
  • Java ArrayList size() Method
  • Java ArrayList sort() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList subList() Method
  • Java ArrayList toArray() Method
  • Java ArrayList trimToSize() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme