October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Java ArrayList Size vs. Array Length: What Each Count Means

Use size() for an ArrayList’s current elements and length for an array’s components. The two counts are different from an ArrayList’s backing capacity.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a Java ArrayList, use list.size() to get the number of elements currently in the list. For an array, use array.length to get the number of components. size() is a method; length is a field. An ArrayList’s storage capacity is separate from its element count.

ArrayList size vs. array length

What you want to count Java expression What it means
Elements in an ArrayList list.size() The elements currently in the list
Components in an array array.length The number of components in that array object
Backing storage for an ArrayList Not returned by size() Capacity is the internal storage available to the list and is at least its current size

For example:

ArrayList<String> names = new ArrayList<>();
names.add("Ada");
names.add("Linus");

int listElementCount = names.size(); // 2

String[] fixedNames = {"Ada", "Linus"};
int arrayComponentCount = fixedNames.length; // 2

Both expressions produce 2 here, but they count different structures through different APIs. Oracle’s Java SE 26 ArrayList API defines size() as the number of elements in the list. The Java Language Specification, Java SE 26, §10.7 defines an array’s public final length field as the number of its components.

As an Amazon Associate I earn from qualifying purchases.

What changes when elements are added or removed?

An ArrayList’s size can change

An ArrayList is a resizable-array implementation of List. Adding or removing elements changes its current size, which size() reports.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

An array’s length stays fixed

An array’s length does not change during that array object’s lifetime. You can make a variable refer to a different array, but that does not resize the original array.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Size is not ArrayList capacity

Capacity describes the size of the array an ArrayList uses internally to store elements. It is always at least the list’s current size and may grow as elements are added. The API does not specify a particular growth policy, so do not infer capacity from size().

For example, constructing a list with an initial capacity reserves storage; it does not mean the list already contains that many elements. The API documents ensureCapacity and trimToSize as capacity-related controls. Use them when managing storage is relevant, rather than treating capacity as the element count.

Use the right expression in checks and loops

  • To check whether an ArrayList is empty, prefer its isEmpty() method. Use size() when you need the count or a loop boundary.
  • For an array, use array.length. An array does not provide size(); an ArrayList does not expose an array-style length field.
  • A count is not the last valid index. With n elements or components, valid indices run from 0 through n - 1. A conventional loop therefore tests i < count.
for (int i = 0; i < names.size(); i++) {
    System.out.println(names.get(i));
}

for (int i = 0; i < fixedNames.length; i++) {
    System.out.println(fixedNames[i]);
}

Common mistakes to avoid

  • Calling array.size(): arrays use the length field.
  • Writing list.length: an ArrayList uses size() to report its current element count.
  • Using an initial capacity as if it were the number of elements already present.
  • Using i <= count as a loop condition, which attempts to access one index beyond the valid range.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.