Java ArrayList.size() – Examples

In this tutorial, we will learn about the Java ArrayList.size() method, and learn how to use this method to get the size of this ArrayList, with the help of examples.

size()

ArrayList.size() returns the number of elements in this ArrayList.

Syntax

The syntax of size() method is

ArrayList.size()

Returns

The method returns integer value.

Example 1 – size()

In this example, we will initialize an ArrayList with four string elements "a", "b", "c" and "d". To get the size of this ArrayList programmatically, we will use ArrayList.size() method. As there are four elements in this ArrayList, size() method should return the integer value of 4.

Java Program

import java.util.ArrayList;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>();  
		arrayList.add("a");  
		arrayList.add("b");  
		arrayList.add("c"); 
		arrayList.add("d");
		
		int size = arrayList.size();
		System.out.println("Size of the ArrayList is : " + size);
	}
}

Output

Size of the ArrayList is : 4

Example 3 – size() – Modify ArrayList and Find Size

In this example, we will initialize an ArrayList with four string elements "a", "b", "c" and "d". ArrayList.size() returns 4 as there are fourn elements in this ArrayList. We will add an element to this ArrayList, and get the size of the modified ArrayList, which should be 4+1 = 5. After that, we will remove two elements at the beginning of ArrayList. The new size is 5-2 = 3.

Java Program

import java.util.ArrayList;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>();  
		arrayList.add("a");  
		arrayList.add("b");  
		arrayList.add("c"); 
		arrayList.add("d");
		
		int size = arrayList.size();
		System.out.println("Size of the ArrayList is : " + size);
		
		arrayList.add("d");
		size = arrayList.size();
		System.out.println("After adding one element..");
		System.out.println("Size of the ArrayList is : " + size);
		
		arrayList.remove(0);
		arrayList.remove(0);
		size = arrayList.size();
		System.out.println("After removing two elements..");
		System.out.println("Size of the ArrayList is : " + size);
	}
}

Output

Size of the ArrayList is : 4
After adding one element..
Size of the ArrayList is : 5
After removing two elements..
Size of the ArrayList is : 3

Example 3 – size() – Empty ArrayList

In this example, we will take an empty ArrayList and find its size using ArrayList.size() method. Since the ArrayList is empty, size() should return 0.

Java Program

import java.util.ArrayList;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>();  
		int size = arrayList.size();
		System.out.println("Size of the ArrayList is : " + size);
	}
}

Output

Size of the ArrayList is : 0

Conclusion

In this Java Tutorial, we have learnt the syntax of Java ArrayList.size() method, and also learnt how to use this method with the help of examples.