Open In App

Array vs ArrayList in Java

Last Updated : 24 Mar, 2025
Comments
Improve
Suggest changes
121 Likes
Like
Report

In Java, an Array is a fixed-sized, homogenous data structure that stores elements of the same type whereas, ArrayList is a dynamic-size, part of the Java Collections Framework and is used for storing objects with built-in methods for manipulation. The main difference between array and ArrayList is:

  • Array: Arrays have a fixed size and provide fast performance.
  • ArrayList: ArrayList is dynamic in size and provides more flexibility and built-in methods.

Difference Between Array and ArrayList

The difference between Array and ArrayList is listed below:

Aspect

Array

ArrayList

Dimensionality

An array can be single-dimensional or multi-dimensional

ArrayList can be only a single-dimensional

Traversing Elements

Uses for and foreach loop for iteration

uses for-each, Iteraor or ListIterator

Length/Size

The length keyword gives the total size of the array

size() method returns the number of elements in the ArrayList

Size

Array size is static and fixed length

ArrayList size is dynamic

Speed

Array speed is fast due to the fixed size

ArrayList speed is relatively slower due to resizing and dynamic behaviour

Primitive Data Storage

Directly stores primitive data types (e.g., int, double)

Primitive data types are not directly added to unlikely arrays, they are added indirectly with the help of autoboxing and unboxing

Generics

Not supported, making arrays type-unsafe

Supports generics, making ArrayList type-safe

Adding Elements

Uses assignment (arr[0] = value)

Uses add() method

Note: ArrayList in Java (equivalent to vector in C++) has a dynamic size. It can be shrunk or expanded based on size. ArrayList is a part of the collection framework and is present in Java.util package

Base 1: An array is a basic functionality provided by Java. ArrayList is part of the collection framework in Java. Therefore array members are accessed using [], while ArrayList has a set of methods to access elements and modify them. 

Example:


Output
1
1


Base 2: The array is a fixed-size data structure while ArrayList is not. One need not mention the size of the ArrayList while creating its object. Even if we specify some initial capacity, we can add more elements.

Example:


Output
[1, 2, 3, 4]
[1, 2, 3]


Base 3: An array can contain both primitive data types as well as objects of a class depending on the definition of the array. However, ArrayList only supports object entries, not primitive data types. 

Note: When we do arraylist.add(1) than it converts the primitive int data type into an Integer object which is as illustrated in below example.

Example:


Output
Successfully compiled and executed

Base 4: Since ArrayList can not be created for primitive data types, members of ArrayList are always references to objects at different memory locations (See this for details). Therefore in ArrayList, the actual objects are never stored at contiguous locations. References of the actual objects are stored at contiguous locations. 

On the other hand, in the array, it depends on whether the array is of primitive type or object type. In the case of primitive types, actual values are contiguous locations, but in the case of objects, allocation is similar to ArrayList. Java ArrayList supports many additional operations like indexOf(), remove(), etc. These functions are not supported by Arrays.



Next Article

Similar Reads