Java ArrayList Size() Method

ArrayList Size() Method in Java

The java.util.ArrayList.size() method can be used to determine an array list’s size since it returns the array list’s size, or the number of elements in it. This page is all about Java ArrayList Size() Method.

ArrayList differs from the array’s length that serves as the ArrayList’s backing array, which is known as the capacity of the ArrayList.

Java ArrayList Size() Method

Obtain the size of an ArrayList in Java

The declaration for the java.util.ArrayList.size() method is provided below.

public int size()

Here are some of the important pointers about ArrayList Size() in Java:-

  • There are no parameters required for the size() method.
  • The arraylist’s size() method returns the total number of entries.
  • Additionally, a new ArrayList is known as an empty ArrayList, and size() will return 0.
  • Size increases incrementally as more parts are added.
  • Additionally, you can empty an ArrayList of all elements by using the clear() method, as seen in our examples.

ArrayList size() Method example in Java

Run
import java.util.*;

public class Main{
public static void main(String[] args){
ArrayList arr = new ArrayList<>();
arr.add(1);
arr.add(2);
arr.add(3);

int size = arr.size(); // using size function
System.out.println(size); // printing size function
}
}

Output

3

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription