← All Lessons Lesson 2 / 68
Lesson 2

Why Do We Need Arrays? The Problem With Using Many Variables

Why Do We Need Arrays? The Problem With Using Many Variables

Before learning what an array is, it helps to first feel the *problem* that arrays were invented to solve. Once you understand the pain, the solution makes perfect sense.

We often need a collection of related data

When you write a program, you very often need to keep a collection of data items — many values that belong together and that you want to go through one after another (this is called accessing them *sequentially*).

A simple example: imagine you want to store the ages of all the students in a class.

A *variable* is just a named box in the computer's memory that holds one value. So your first idea might be to make one variable for each student:

ageStudent1 = 12
ageStudent2 = 13
ageStudent3 = 13

If the class has only 3 students, this is fine. It's easy to read and easy to write.

The problem appears when the numbers grow

Now ask a simple question: what if the class has 108 students? Or 500? Or 10,000?

With the one-variable-per-student approach, you would have to type out a separate, differently-named variable for every single student:

ageStudent1   = 12
ageStudent2   = 13
ageStudent3   = 13
ageStudent4   = 11
ageStudent5   = 11
...
ageStudent105 = 13
ageStudent106 = 11
ageStudent107 = 13
ageStudent108 = 11

This technically works, but it is a nightmare to manage. Imagine writing 108 lines just to store ages — and then writing more code every time you want to look at them. Each variable has its own unique name, so there is no easy way to say "go through all of them in a loop."

Think of it like giving every book in a giant library its own random shelf with a unique label, instead of numbering the shelves in order. Finding or counting books would be painful.

The limitations of using plain variables

Variables are useful, but using them to store *lots* of related data has clear limits:

  • A variable can store only one value at a time. One box, one item.
  • Each variable needs a different name. Even when they all hold the same kind of information (a student's age), you must invent a fresh name for each one.
  • Too many variables make the code messy. Long, repetitive code is hard to read and very easy to get wrong — this is called being *error-prone*.
  • It does not scale. "Scale" means handling bigger and bigger amounts of data. Adding more students means adding more variables by hand, which quickly becomes impossible.

Why this matters

Computers are built to handle data at scale — managing huge amounts of information that no human could track by hand. Problems like the "class of students" example show up constantly, even in the simplest software.

This need is so basic and so common that even the *lowest-level* programming languages — like assembly, which sits very close to the raw machine — have built-in support for a data structure that stores many values together. A *data structure* is simply an organized way to hold a group of related values.

That data structure is the array — and that is exactly what we will learn next.

Why Do We Need Arrays? The Problem With Using Many Variables
Diagram — click to zoom (scroll / drag to pan)