DEV Community

Shankar L
Shankar L

Posted on

Arrays

Why should you care?

Arrays are one of the most fundamental data structures in programming. They let you store multiple values under a single variable name and access those values efficiently using an index.

Arrays appear everywhere in software:

  • Storing student marks
  • Processing images and pixels
  • Representing matrices and tables
  • Implementing stacks, queues, and other data structures
  • Sorting and searching data
  • Managing collections of values in memory

Understanding arrays also gives you a better understanding of memory, pointers, indexing, and how higher-level data structures work.


The Problem

Imagine you need to store the marks of 5 students.

Without an array, you might write:

int mark1 = 85;
int mark2 = 92;
int mark3 = 78;
int mark4 = 90;
int mark5 = 88;
Enter fullscreen mode Exit fullscreen mode

This works, but what happens when you have 10,000 students?

You don't want 10,000 separate variables.

We need a way to:

Store many related values together and access each value efficiently.

That's the problem arrays solve.


The Concept

An array is a collection of elements of the same data type, stored in a structured sequence.

For example:

int[] marks = {85, 92, 78, 90, 88};
Enter fullscreen mode Exit fullscreen mode

Conceptually:

Array: marks

Index:    0    1    2    3    4
          ↓    ↓    ↓    ↓    ↓
Value:   85   92   78   90   88
Enter fullscreen mode Exit fullscreen mode

Each element has an index that identifies its position.

Most programming languages use zero-based indexing, meaning the first element is at index 0.

marks[0]   // 85
marks[1]   // 92
marks[4]   // 88
Enter fullscreen mode Exit fullscreen mode

The array therefore provides two important things:

  1. Contiguous/ordered storage of elements
  2. Fast access using an index

Simple Explanation

Think of an array as a row of numbered boxes.

        ┌────┬────┬────┬────┬────┐
Index   │ 0  │ 1  │ 2  │ 3  │ 4  │
        ├────┼────┼────┼────┼────┤
Value   │ 85 │ 92 │ 78 │ 90 │ 88 │
        └────┴────┴────┴────┴────┘
Enter fullscreen mode Exit fullscreen mode

If you want the value at position 2, you simply ask for:

marks[2]
Enter fullscreen mode Exit fullscreen mode

The computer can calculate where that element is located based on:

  • Starting address of the array
  • Index
  • Size of each element

Conceptually:

Address of element
      =
Base Address + (Index × Element Size)
Enter fullscreen mode Exit fullscreen mode

That's why accessing an array element by index is generally O(1) — constant time.


Real-world Analogy

Imagine a row of numbered lockers.

Locker:   0     1     2     3     4
         ┌───┬───┬───┬───┬───┐
         │85 │92 │78 │90 │88 │
         └───┴───┴───┴───┴───┘
Enter fullscreen mode Exit fullscreen mode

If someone tells you:

"The value you need is in locker 3."

You can immediately go to locker 3.

You don't need to open lockers 0, 1, and 2 first.

That's the key idea behind array indexing.


Code Example

Here's a simple Java example:

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

        int[] marks = {85, 92, 78, 90, 88};

        System.out.println(marks[0]);
        System.out.println(marks[2]);

        marks[2] = 80;

        System.out.println(marks[2]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

85
78
80
Enter fullscreen mode Exit fullscreen mode

Notice this:

marks[2] = 80;
Enter fullscreen mode Exit fullscreen mode

We changed the third element from 78 to 80.

We can also loop through the entire array:

for (int i = 0; i < marks.length; i++) {
    System.out.println(marks[i]);
}
Enter fullscreen mode Exit fullscreen mode

Here:

marks.length
Enter fullscreen mode Exit fullscreen mode

gives the number of elements in the array.

The loop visits:

i = 0 → marks[0]
i = 1 → marks[1]
i = 2 → marks[2]
i = 3 → marks[3]
i = 4 → marks[4]
Enter fullscreen mode Exit fullscreen mode

Creating an array with a fixed size

You can also create an empty array:

int[] marks = new int[5];
Enter fullscreen mode Exit fullscreen mode

This creates space for 5 integers.

Initially, Java initializes the elements to 0:

Index:   0   1   2   3   4
Value:   0   0   0   0   0
Enter fullscreen mode Exit fullscreen mode

You can then assign values:

marks[0] = 85;
marks[1] = 92;
Enter fullscreen mode Exit fullscreen mode

Common Mistakes

Mistake 1: Forgetting that indexing starts at 0

Beginners often think the first element is:

marks[1]
Enter fullscreen mode Exit fullscreen mode

But in a zero-indexed array:

marks[0] → first element
marks[1] → second element
marks[2] → third element
Enter fullscreen mode Exit fullscreen mode

So an array containing 5 elements has indexes:

0, 1, 2, 3, 4
Enter fullscreen mode Exit fullscreen mode

Not:

1, 2, 3, 4, 5
Enter fullscreen mode Exit fullscreen mode

Mistake 2: Accessing outside the array

If an array has 5 elements:

int[] marks = new int[5];
Enter fullscreen mode Exit fullscreen mode

The last valid index is:

marks[4]
Enter fullscreen mode Exit fullscreen mode

Trying:

marks[5]
Enter fullscreen mode Exit fullscreen mode

causes an ArrayIndexOutOfBoundsException in Java.

The general rule is:

0 ≤ index < array.length
Enter fullscreen mode Exit fullscreen mode

Mistake 3: Assuming arrays automatically grow

A Java array has a fixed length.

If you create:

int[] numbers = new int[5];
Enter fullscreen mode Exit fullscreen mode

you cannot turn that same array into a 6-element array.

If you need a dynamically growing collection in Java, you would typically use something like:

ArrayList<Integer>
Enter fullscreen mode Exit fullscreen mode

instead.


Advanced Notes

1. Arrays and memory

Arrays are closely connected to memory.

Suppose an integer occupies 4 bytes and an array begins at address 1000:

Index 0 → 1000
Index 1 → 1004
Index 2 → 1008
Index 3 → 1012
Enter fullscreen mode Exit fullscreen mode

The address can be calculated using:

Address = Base + (Index × Element Size)
Enter fullscreen mode Exit fullscreen mode

This is one of the fundamental reasons indexed array access is fast.


2. Time complexity

For a typical array:

Operation Complexity
Access by index O(1)
Update by index O(1)
Search O(n)
Insert at beginning O(n)
Insert at middle O(n)
Delete from middle O(n)

Why is insertion expensive?

Suppose we have:

10 20 30 40
Enter fullscreen mode Exit fullscreen mode

and want to insert 25 between 20 and 30.

Elements may need to move:

10 20 25 30 40
      ↑
Enter fullscreen mode Exit fullscreen mode

Several elements must be shifted to make room.


3. One-dimensional vs multidimensional arrays

A normal array is one-dimensional:

int[] numbers = {10, 20, 30};
Enter fullscreen mode Exit fullscreen mode

You can also have a two-dimensional array:

int[][] matrix = {
    {1, 2, 3},
    {4, 5, 6}
};
Enter fullscreen mode Exit fullscreen mode

Conceptually:

       Column
       0  1  2
     ┌─────────
Row 0│ 1  2  3
Row 1│ 4  5  6
Enter fullscreen mode Exit fullscreen mode

You access an element using two indexes:

matrix[1][2]
Enter fullscreen mode Exit fullscreen mode

which gives:

6
Enter fullscreen mode Exit fullscreen mode

4. Arrays are not the same in every language

The fundamental idea is similar, but implementation details differ.

For example:

  • C arrays are closely tied to contiguous memory and pointer arithmetic.
  • Java arrays are objects with runtime bounds checking.
  • Python's list is a dynamic array-like structure containing references to objects.
  • JavaScript arrays are more flexible and are not simply equivalent to fixed-size C arrays.

So the mental model of indexed elements is universal, but the underlying implementation can differ.


The Bigger Picture

Arrays connect several fundamental computer science concepts.

You can think of the progression like this:

Variables
   ↓
Memory
   ↓
Arrays
   ↓
Pointers / References
   ↓
Data Structures
   ↓
Algorithms
Enter fullscreen mode Exit fullscreen mode

Arrays provide the foundation for many important structures.

For example:

Array
 ├── Stack
 ├── Queue
 ├── Heap
 ├── Hash-table storage
 └── Dynamic arrays
Enter fullscreen mode Exit fullscreen mode

And many algorithms operate directly on arrays:

Searching
Sorting
Two Pointers
Sliding Window
Binary Search
Prefix Sums
Dynamic Programming
Enter fullscreen mode Exit fullscreen mode

Learning arrays well is therefore not just about learning one data structure.

It is about understanding how programs organize and manipulate collections of data.


The Most Important Mental Model

An array is a sequence of elements where the index lets you jump directly to a position.

Keep this picture in your head:

Base Address
     ↓
┌────┬────┬────┬────┬────┐
│ 10 │ 20 │ 30 │ 40 │ 50 │
└────┴────┴────┴────┴────┘
  ↑
index 0

Address of element:
Base + (index × element size)
Enter fullscreen mode Exit fullscreen mode

The index is not the value.

It tells the computer which element you want.


Summary

Arrays allow us to store multiple related values in an organized sequence.

The key ideas are:

  • An array stores multiple elements.
  • Elements are accessed using indexes.
  • Most languages use zero-based indexing.
  • Array access by index is typically O(1).
  • Fixed-size arrays cannot automatically grow.
  • Inserting or deleting elements can require shifting other elements.
  • Arrays are fundamental building blocks for many other data structures and algorithms.
  • Understanding arrays helps connect high-level programming with memory and address calculations.

If you understand how an array maps an index to a location in memory, you've taken one of the first major steps toward understanding how data structures actually work inside a computer.

Top comments (0)