VOOZH about

URL: https://www.geeksforgeeks.org/java/array-vs-arraylist-in-java/

⇱ Array vs ArrayList in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Array vs ArrayList in Java

Last Updated : 5 Jun, 2026

In Java, both Array and ArrayList are used to store multiple elements of the same type, but they differ in structure and flexibility. An array has a fixed size, while ArrayList is dynamic and part of the Java Collections Framework, offering more built-in methods for easy data manipulation.

  • Array has a fixed length, ArrayList can grow and shrink dynamically.
  • Array is faster and memory-efficient; ArrayList provides more flexibility with built-in methods.
  • Array stores primitive types and objects; ArrayList stores only objects (uses wrapper classes for primitives).

Array

An Array is a fixed-size data structure used to store multiple elements of the same data type in contiguous memory locations. Each element is accessed using its index, starting from 0.

  • Elements are stored in sequential memory locations, allowing fast access using indexes.
  • Supports both primitive data types (int, char, double, etc.) and objects.
  • Length of an array can be accessed using the length property.

Syntax

dataType[] arrayName; // Declaration
dataType[] arrayName = new dataType[size]; // Declaration and Creation
int[] numbers = {10, 20, 30, 40, 50}; // Declaration, Creation, and Initialization

ArrayList

ArrayList is a resizable implementation of the List interface in the Java Collections Framework that stores elements dynamically. It automatically grows or shrinks as elements are added or removed.

  • Provides many built-in methods such as add(), remove(), get(), and contains().
  • Maintains insertion order and allows duplicate elements.
  • Stores only objects; for primitive values, wrapper classes such as Integer, Double, and Character are used.

Note: ArrayList is a dynamic-size data structure in Java that can automatically grow or shrink as elements are added or removed. It is part of the Java Collections Framework. 

Syntax

ArrayList<String> list = new ArrayList<>();

Some Common Differences

The following common differences between Array and ArrayList are listed below with the help of examples.

1. Difference in Accessing Elements

Array uses the [] operator to access elements, whereas ArrayList uses methods such as get(), add(), and remove().


Output
1
1

2. Difference in Size and Flexibility

Array has a fixed size, whereas ArrayList is dynamic and can grow or shrink as needed.


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

3. Difference in Data Type Support

Array supports both primitive data types and objects, whereas ArrayList supports only objects.

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.


Output
Successfully compiled and executed

Array vs ArrayList

FeatureArrayArrayList
DimensionalityCan be single-dimensional or multi-dimensional.Supports only single-dimensional structures.
Traversing ElementsUses for loop and enhanced for-each loop.Uses for, for-each, Iterator, and ListIterator.
Length / SizeUses the length property to get the size.Uses the size() method to get the number of elements.
Size NatureFixed size; cannot be changed after creation.Dynamic size; can grow or shrink automatically.
PerformanceFaster due to fixed size and direct memory access.Slightly slower because of dynamic resizing and additional operations.
Primitive Data SupportStores both primitive data types and objects.Stores only objects (wrapper classes are used for primitives).
Generics SupportDoes not support generics.Supports generics, providing type safety.
Adding ElementsElements are added using index assignment (arr[0] = value).Elements are added using the add() method.
Comment