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().
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));
}
}
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.
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]
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.
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");
}
}
Output
Successfully compiled and executed
Array vs ArrayList
| Feature | Array | ArrayList |
|---|---|---|
| Dimensionality | Can be single-dimensional or multi-dimensional. | Supports only single-dimensional structures. |
| Traversing Elements | Uses for loop and enhanced for-each loop. | Uses for, for-each, Iterator, and ListIterator. |
| Length / Size | Uses the length property to get the size. | Uses the size() method to get the number of elements. |
| Size Nature | Fixed size; cannot be changed after creation. | Dynamic size; can grow or shrink automatically. |
| Performance | Faster due to fixed size and direct memory access. | Slightly slower because of dynamic resizing and additional operations. |
| Primitive Data Support | Stores both primitive data types and objects. | Stores only objects (wrapper classes are used for primitives). |
| Generics Support | Does not support generics. | Supports generics, providing type safety. |
| Adding Elements | Elements are added using index assignment (arr[0] = value). | Elements are added using the add() method. |