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
Array of ArrayList in Java
We often come across 2D arrays where most of the part in the array is empty. Since space is a huge problem, we try different things to reduce the space. One such solution is to use jagged array when we know the length of each row in the array, but the problem arises when we do not specifically know
2 min read
ArrayList of ArrayList in Java
We have discussed that an array of ArrayList is not possible without warning. A better idea is to use ArrayList of ArrayList. Java // Java code to demonstrate the concept of // array of ArrayList import java.util.*; public class Arraylist { public static void main(String[] args) { int n = 3; // Here
1 min read
ArrayList in Java
Java ArrayList is a part of the collections framework and it is a class of java.util package. It provides us with dynamic-sized arrays in Java. The main advantage of ArrayList is that, unlike normal arrays, we don't need to mention the size when creating ArrayList. It automatically adjusts its capac
9 min read
Array to ArrayList Conversion in Java
In Java, arrays are fixed-sized, whereas ArrayLists are part of the Java collection Framework and are dynamic in nature. Converting an array to an ArrayList is a very common task and there are several ways to achieve it.Methods to Convert Array to an ArrayList1. Using add() Method to Manually add th
3 min read
Vector vs ArrayList in Java
ArrayList and Vectors both implement the List interface, and both use (dynamically resizable) arrays for their internal data structure, much like using an ordinary array. Syntax: ArrayList: ArrayList<T> al = new ArrayList<T>(); Vector: Vector<T> v = new Vector<T>(); ArrayList
4 min read
Conversion of Array To ArrayList in Java
Following methods can be used for converting Array To ArrayList: Method 1: Using Arrays.asList() method Syntax: public static List asList(T... a) // Returns a fixed-size List as of size of given array. // Element Type of List is of same as type of array element type. // It returns an List containing
5 min read
Java ArrayList of Arrays
ArrayList of arrays can be created just like any other objects using ArrayList constructor. In 2D arrays, it might happen that most of the part in the array is empty. For optimizing the space complexity, Arraylist of arrays can be used. ArrayList<String[ ] > geeks = new ArrayList<String[ ]
2 min read
ArrayList vs LinkedList in Java
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. However, the limitation of the array is that the size of the array is predefined and fixed. There are multiple ways to solve this problem. In this article, the diff
5 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
Arrays Class in Java
The Arrays class in java.util package is a part of the Java Collection Framework. This class provides static methods to dynamically create and access Java arrays. It consists of only static methods and the methods of an Object class. The methods of this class can be used by the class name itself.The
15 min read