Array vs. ArrayList in Java — Explaining the Differences

Alexander Obregon
6 min readFeb 16, 2023
Image Source

Introduction

Depending on how familiar you are with Java, you’ve may have encountered the terms “Array” and “ArrayList” before. While they may seem interchangeable, there are some key differences between the two data structures that you should be aware of. In this article, we’ll explore the differences between Arrays and ArrayLists in Java and provide recommendations on when to use each.

What Are Arrays and ArrayLists?

Arrays and ArrayLists are both used to store collections of data in Java. However, there are several key differences between the two. Arrays are a fixed-size collection of data, while ArrayLists are dynamically resizable. Additionally, ArrayLists are part of the Java Collections Framework, while arrays are a basic Java feature.

Declaration and Initialization

The syntax for declaring an array and an ArrayList is similar, but there are some key differences. Arrays are declared using square brackets, while ArrayLists use angle brackets. Additionally, arrays must be initialized with a fixed size, while ArrayLists can be initialized with or without an initial capacity.

Array Declaration Example:

// Declaring an array of integers with a fixed length of 5
int[] numbers = new int[5];

// Declaring an array of strings with an unspecified length
String[] names;

ArrayList Declaration Example:

        // First we must import the java.util.ArrayList
import java.util.ArrayList;

public class ArrayListExample {
public static void main(String[] args) {

// Declaring an ArrayList of integers
ArrayList<Integer> numbers = new ArrayList<Integer>();
// Declaring an ArrayList of strings
ArrayList<String> words= new ArrayList<String>();
}
}

Memory Allocation and Size

When an array is declared, Java allocates a block of memory for the array, and the size of the memory block is fixed. On the other hand, when an ArrayList is created, Java creates an array with an initial capacity, and as more elements are added to the ArrayList, the array is dynamically resized to accommodate them.

Insertion and Deletion

Insertion and Deletion The process of inserting and deleting elements from an array and an ArrayList is significantly different due to their structure. In an array, you cannot directly insert or delete elements due to the fixed size of the array. However, you can manually shift elements to simulate these operations. In contrast, ArrayLists come with built-in methods for insertion and deletion, which automatically adjust the size of the ArrayList to accommodate or remove elements.

Array Insertion Example:

// Declaring an array of integers with a fixed length of 5
int[] numbers = new int[5];

// Inserting an element at the beginning of the array
numbers[0] = 10;

// Inserting an element at the end of the array
numbers[numbers.length - 1] = 20;

// Inserting an element at a specific index in the array
int index = 2;
int value = 15;
for (int i = numbers.length - 1; i > index; i--) {
numbers[i] = numbers[i - 1];
}
numbers[index] = value;

In this example, we declare an array of integers and insert elements at the beginning, end, and a specific index in the array. To insert an element at a specific index, we loop through the array in reverse order and shift each element to the right to make room for the new element, then insert the new element at the desired index.

Array Deletion Example:

// Declaring an array of integers with a fixed length of 5
int[] numbers = {10, 15, 20, 25, 30};

// Deleting an element at the beginning of the array
for (int i = 0; i < numbers.length - 1; i++) {
numbers[i] = numbers[i + 1];
}
numbers[numbers.length - 1] = 0;

// Deleting an element at the end of the array
numbers[numbers.length - 1] = 0;

// Deleting an element at a specific index in the array
int index = 2;
for (int i = index; i < numbers.length - 1; i++) {
numbers[i] = numbers[i + 1];
}
numbers[numbers.length - 1] = 0;

In this example, we declare an array of integers and delete elements from the beginning, end, and a specific index in the array. To delete an element at a specific index, we loop through the array starting at the index to be deleted and shift each element to the left to fill the gap, then set the last element in the array to 0 to remove the duplicate element at the end of the array.

ArrayList Insertion Example:


// Declaring an ArrayList of integers
ArrayList<Integer> numbers = new ArrayList<Integer>();

// Adding elements to the ArrayList
numbers.add(10);
numbers.add(20);
numbers.add(30);

// Inserting an element at the beginning of the ArrayList
numbers.add(0, 5);

// Inserting an element at the end of the ArrayList
numbers.add(numbers.size(), 40);

// Inserting an element at a specific index in the ArrayList
int index = 2;
int value = 15;
numbers.add(index, value);

In this example, we declare an ArrayList of integers and insert elements at the beginning, end, and a specific index in the ArrayList. To insert an element at a specific index, we use the add() method with two arguments - the index at which to insert the element, and the value to be inserted.

ArrayList Deletion Example:


// Declaring an ArrayList of integers
ArrayList<Integer> numbers = new ArrayList<Integer>();

// Adding elements to the ArrayList
numbers.add(10);
numbers.add(20);
numbers.add(30);

// Removing an element at the beginning of the ArrayList
numbers.remove(0);

// Removing an element at the end of the ArrayList
numbers.remove(numbers.size() - 1);

// Removing an element at a specific index in the ArrayList
int index = 1;
numbers.remove(index);

In this example, we declare an ArrayList of integers and delete elements from the beginning, end, and a specific index in the ArrayList. To delete an element at a specific index, we use the remove() method with the index of the element to be deleted.

Access and Iteration

Accessing and iterating over elements in an array and an ArrayList is similar. However, accessing an element in an array is faster than accessing an element in an ArrayList because the elements in an array are stored in contiguous memory locations. Additionally, iterating over an ArrayList can be slower than iterating over an array because ArrayLists use an iterator object to traverse the elements.

Array Iterator Example:

// Declaring an array of integers with a fixed length of 5
int[] numbers = {10, 20, 30, 40, 50};

// Iterating over the array using a for loop
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}

In this example, we declare an array of integers and use a for loop to iterate over the elements in the array. The loop initializes a counter variable i to 0, which is the index of the first element in the array. The loop condition is i < numbers.length, which ensures that the loop continues until the counter reaches the length of the array. The loop increments the counter by 1 in each iteration using the i++ expression.

Inside the loop, we use the System.out.println() method to print each element in the array to the console. We access each element using the array indexing syntax numbers[i], which retrieves the value of the element at index i in the array.

ArrayList Iterator Example:

import java.util.ArrayList;

public class ArrayListIterationExample {
public static void main(String[] args) {
// Declaring an ArrayList of integers
ArrayList<Integer> numbers = new ArrayList<Integer>();

// Adding elements to the ArrayList
numbers.add(10);
numbers.add(20);
numbers.add(30);

// Iterating over the ArrayList using a for-each loop
for (Integer number : numbers) {
System.out.println(number);
}
}
}

In this example, we declare an ArrayList of integers and add elements to it using the add() method. We then use a for-each loop to iterate over the elements in the ArrayList. The loop declares a new variable number of type Integer for each element in the ArrayList, and assigns it the value of the element in each iteration. The loop body simply prints the value of the current element to the console using the System.out.println() method.

Using a for-each loop to iterate over an ArrayList is a more concise and readable alternative to using a traditional for loop with an index variable. It also helps to avoid off-by-one errors and simplify the syntax for accessing elements.

Conclusion

The key differences between Arrays and ArrayLists in Java are related to size and flexibility. Arrays are fixed-size, while ArrayLists can dynamically grow or shrink in size. While both data structures have their advantages and disadvantages, the choice of which one to use depends on the specific use case. Generally, if you need a fixed-size collection of data or you know the size of the data in advance, use an array. If you need a dynamically resizable collection of data or you don’t know the size of the data in advance, use an ArrayList.

  1. Array Java Documentation
  2. ArrayList Java Documentation

--

--

Alexander Obregon

Software Engineer, fervent coder & writer. Devoted to learning & assisting others. Connect on LinkedIn: https://www.linkedin.com/in/alexander-obregon-97849b229/