Search⌘ K
AI Features

Common Array Operations

Explore how to perform essential array operations such as access, insertion, update, and deletion in Java. Understand how insertions and deletions affect array structure and learn their time and space complexities to write efficient code handling arrays dynamically.

Before writing any algorithm, we need to know what we can actually do with an array. Arrays support a small, fixed set of operations. Each one is simple to learn, but together they cover almost everything we will ever need: reading values, adding new ones, changing them, removing old ones, and scanning through the entire collection.

In this lesson, we will go through each operation one by one.

Access (Read an element)

The most basic thing you can do with an array is read a value stored in it. This is called accessing an element. Think of it like looking up a specific game on our scorecard.

Syntax:

To access an element, we write the array name followed by the index of the element we want to access in square brackets:

Shell
array[index]

Example: Consider the following scores array: [88, 95, 70, 82, 91]. If we want to access the score of game 3, we will use scores[2], which will give us 70.

Accessing the element at index 2
Accessing the element at index 2

Java implementation:

The following code shows how to access the score at index 2 from the scores array:

Java 25
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] scores = {88, 95, 70, 82, 91};
System.out.println(scores[2]); // 70
}
}
Access an element

Note: Trying to access an index that does not exist, for example, scores[10] in a five-element array, will throw an ArrayIndexOutOfBoundsException.

Insertion (Add an element)

Insertion means adding a new element to the array. When you insert an element, the array grows by one. However, not all insertions are equal. The cost of insertion depends on where the new element goes in the array.

Insert at the end

Adding an element to the end of the array is the simplest kind of insertion. Because the new element goes into the next empty slot at the end, nothing else in the array needs to move. Think of it like adding a new element to the end of our scorecard and writing the score in.

Note: Since Java static arrays are fixed in size, we use ArrayList for dynamic insertion.

Syntax:

To add an element to the end, we use the following syntax:

list.add(value)

Example: Consider a scores array [88, 95, 70, 82]. A fifth game has been played, and the score 91 needs to be recorded. The visualization below shows how a new element is added to the end of the array:

Java implementation:

The following code adds a new score 91 to the end of the scores array:

Java 25
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<>();
scores.add(88);
scores.add(95);
scores.add(70);
scores.add(82);
scores.add(91);
System.out.println(scores); // [88, 95, 70, 82, 91]
}
}
Insert an element at the end of the array

The scorecard originally had four games: [88, 95, 70, 82]. A fifth game was played, and the score 91 needs to be recorded. Adding it places the new score at the end of the scorecard, giving [88, 95, 70, 82, 91]. The four previously recorded scores remain exactly where they were.

Insert in the middle or beginning (requires shifting)

If we want to insert an element somewhere other than the end, the computer first needs to create a gap at the right position. To do that, it shifts every element from the insertion point onward one position to the right, and then places the new value into the freed-up slot. This is similar to squeezing a new row into the middle of our scorecard and pushing everything below it down.

Syntax:

To insert an element at a specific position, we use the following syntax. The first argument specifies where to place the new element, and the second specifies the value itself.

array.add(index, value)

Example: Consider a scores array [88, 95, 70, 82]. A new score of 100 needs to be inserted at index 1. The diagram below shows the state of the scores array after 100 is inserted at index 1: