ArrayList size() method in Java

The size() method is used to know about the size of the ArrayList and in this post, we are going to look at the size() method in detail.

  • Method declaration – public int size()
  • What does it do? It will tell the number of elements present in the ArrayList
  • What does it return? It will return an integer value representing the size of the ArrayList
Code Example
public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();

		// adding elements to the list
		al.add("first");
		al.add("second");

		System.out.println("ArrayList size: " + al.size());

		// adding one more element
		al.add("third");

		System.out.println("ArrayList size now: " + al.size());

	}
}

Output –

ArrayList size: 2
ArrayList size now: 3

As we saw in the above program, that the size of the list increased by one after adding one element into it using the add() method.

Internal implementation of the size() method
    public int size() {
        return size;
    }

Here, the size() method is just returning the size variable of the ArrayList class as we cannot directly access the size variable because it is private.

    /**
     * The size of the ArrayList (the number of elements it contains).
     *
     * @serial
     */
    private int size;
size() vs isEmpty() method

isEmpty() method also uses the size variable to check whether the list is empty or not.

    public boolean isEmpty() {
        return size == 0;
    }

isEmpty() method only tells whether a list contains any elements or not. It doesn’t tell about how many elements are present within the list but the size() method will tell us about the number of elements present within the list.

The What If scenarios

What if we added null elements into the ArrayList and then used the size() method on it?

The null element will be treated as a normal element and will be counted in the size of the ArrayList as illustrated by the below program.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();

		// adding null elements
		al.add(null);
		al.add(null);
		al.add(null);

		System.out.println("ArrayList contents: " + al);
		System.out.println("ArrayList size: " + al.size());

	}
}

Output –

ArrayList contents: [null, null, null]
ArrayList size: 3
What if we try to use the size() method on a null ArrayList?

In this case, it will throw a NullPointerException.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();

		al = null;

		System.out.println("ArrayList size: " + al.size());

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.size()" because "al" is null

Please visit this link if you want to know more about the ArrayList class of java and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.