Array vs ArrayList in Java
Last Updated :
24 Mar, 2025
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:
Java
// Java program to demonstrate differences between
// Array and ArrayList
// Importing required classes
import java.util.ArrayList;
import java.util.Arrays;
// Main class
class Geeks {
// Main driver method
public static void main(String args[])
{
// Input array
int[] arr = new int[2];
arr[0] = 1;
arr[1] = 2;
// Printing first element of array
System.out.println(arr[0]);
// ArrayList
// Creating an arrayList with
// initial capacity say bi it 2
ArrayList<Integer> al = new ArrayList<Integer>(2);
// Adding elements to ArrayList
// using add() method
al.add(1);
al.add(2);
// Printing alongside accessing
// elements of ArrayList
System.out.println(al.get(0));
}
}
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:
Java
// Java program to demonstrate differences between
// Array and ArrayList
import java.util.ArrayList;
import java.util.Arrays;
// Main class
class Geeks {
public static void main(String args[])
{
// Normal Array
// Need to specify the size for array
int[] arr = new int[3];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
// We cannot add more elements to array arr[]
// ArrayList
// Need not to specify size
// Declaring an Arraylist of Integer type
ArrayList<Integer> al = new ArrayList<Integer>();
// Adding elements to ArrayList object
al.add(1);
al.add(2);
al.add(3);
al.add(4);
// We can add more elements to arrL
// Print and display Arraylist elements
System.out.println(al);
// Print and display array elements
System.out.println(Arrays.toString(arr));
}
}
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:
Java
// Java Program to demonstrates how array stores primitive
// and objects and ArrayList requires wrapper classes
import java.util.ArrayList;
class Geeks
{
public static void main(String args[])
{
// allowed
int[] array = new int[3];
// allowed, however, need to be initialized
Geeks[] array1 = new Geeks[3];
// not allowed (Uncommenting below line causes
// compiler error)
// ArrayList<char> arrL = new ArrayList<char>();
// Allowed
ArrayList<Integer> arrL1 = new ArrayList<>();
ArrayList<String> arrL2 = new ArrayList<>();
ArrayList<Object> arrL3 = new ArrayList<>();
System.out.println("Successfully compiled and executed");
}
}
OutputSuccessfully 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.
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read