• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Difference Between Length of Array and Size of ArrayList in Java

July 23, 2023 //  by Talha Malik

Data Allocation in the containers i.e., Array, ArrayList, HashMap, etc. are vital to store and retrieve the data appropriately. Likewise, logging the length or size of these containers is also helpful. This approach is needed when the developer needs to evaluate the allocated space in the memory or analyze the entered elements in a target container. In such a scenario, computing the “length” of an Array or “size” of an ArrayList comes into effect that performs the discussed functionalities appropriately.

This guide will state the differences between the Array’s “length” and “size” of ArrayList.

What is the Difference Between the Length of the Array and the Size of ArrayList in Java?

An Array has a “length” property which returns the length of an Array. It corresponds to the total space assigned in memory during initializing the array. An ArrayList, however, does not have a “length” property. It is such that it comprises a “size()” method for ArrayList which returns the total number of objects contained in the collection.

The following examples will demonstrate the discussed concept and the core differences between both the Array’s “length” and ArrayList’s “size”.

Example 1: Computing the Length of the Array

This example computes the length of an Array using the “length” property:

public class Arraylength {
public static void main(String[] args){
  Integer[] x = new Integer[5];
  x[0] = 1;
  x[1] = 2;
  x[2] = 3;
  System.out.println("Array Length -> " +x.length);
}}

In this code snippet:

  • Define an array of “Integer” type having a defined length of “5”.
  • After that, add the stated integers in the array via indexing.
  • Lastly, return the Array’s length via the “length” property.

Output

In the output, it can be visualized that the defined length of the array is returned instead of the appended values. It is such that the length is independent of the items placed in an array. Rather, it applies a check on the space allocated during array initialization i.e., “5” in this case.

Import the following package in the next example to work with the “ArrayList” class:

import java.util.ArrayList;

Example 2: Computing the Size of the ArrayList

In this specific example, the size of an ArrayList can be calculated via the “size()” method:

public class Arraylength {
public static void main(String[] args){
  ArrayList<Integer> x = new ArrayList<Integer>();
  x.add(1);
  x.add(2);
  x.add(3);
  System.out.println("ArrayList Size -> " +x.size());
}}

According to this code implementation:

  • Create an “Integer” type ArrayList.
  • Now, insert the stated integers in it via the “add()” method.
  • Finally, retrieve the size of the ArrayList using the “size()” method.

Output

This outcome implies that the size of the ArrayList is returned based on the inserted values in it instead i.e., “3” in this scenario.

Conclusion

In Java, the “length” of the Array refers to the space allocated while initializing the Array whereas the “size” of ArrayList is returned based on the contained elements in the ArrayList. It is such that the former property i.e., “length” cannot be applied on the ArrayList and likewise, the latter method i.e., “size()” is not applicable to Array.

Category: Java

Previous Post: « What is ScheduledExecutorService Interface in Java
Next Post: How to Store Unicode Characters in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact