Arrays
Task: Read every value in order
Why we need arrays
Analogy
- Imagine a row of lockers in a school hallway.
- Each locker has a number (the index) and holds one item (the value).
- To find your book in locker 3, you go straight to locker number 3, no searching, no checking the other lockers on the way.
- That is exactly how an array stores and reaches data, related items kept together in one organised line.
What is an array?
Definition
An array is a collection of elements of the same type, stored side by side in memory.
int numbers[3] = {10, 20, 30};Here numbers[0] is 10, numbers[1] is 20, and numbers[2] is 30. One name for all three, and a position you can compute with.
What is an index?
The index is the position of an item. Every element is reached through its index, and arrays start counting from 0, not 1.
int scores[5] = {95, 80, 72, 88, 91};- Index 0 → 95
- Index 1 → 80
- Index 4 → 91
So scores[2] gives 72 - the third value.
Watch out
The beginner mistake: Forgetting that the first element is at index 0.
In an array of 5 items the last index is 4. Reaching for scores[5] runs off the end of the array.
Fixed size
An array's size is decided when it is created, and it does not change.
int marks[5]; // can store exactly 5 integers. Not 4, not 6.You cannot add more elements once it’s full.
Why are arrays fast?
Because every item is the same size and they sit together with no gaps, the computer never has to look for a value. It calculates where the value is and jumps straight there:
address = start + index × size of one item
That is one multiplication, one addition and one read, the same three steps whether the array holds 5 items or 5 million. This is what O(1) access means: reaching any position costs the same.
Adding a new item
- At the end → easy, if there is space. Nothing else moves.
- In the middle → every item from that position onwards has to shift right to open the slot.
Inserting 25 into [10, 20, 30, 40] gives [10, 20, 25, 30, 40], notice how 30 and 40 shifted to make room.
Note
"But I add to lists all the time." You are almost certainly using a dynamic array- ArrayList in Java, vector in C++, list in Python.
It is a plain array with a growth rule bolted on: when it fills up, it quietly allocates a new, bigger block (usually about double) and copies everything across. So it can always accept one more item at the end.
What it does not fix is the shifting. Inserting into the middle of a dynamic array still moves everything after it.
Removing an item
Taking a value out leaves a gap, and a gap breaks the arithmetic that made the index instant. So the remaining items shift left to close it.
Removing 10 from [5, 10, 15, 20] gives [5, 15, 20]. The elements move so they stay together.
| Operation | What happens | Cost |
|---|---|---|
| Read by index | Calculate the address, read it | O(1) |
| Add at the end | Write into the next free slot | O(1) |
| Insert in the middle | Shift everything after it right | O(n) |
| Remove from the middle | Shift everything after it left | O(n) |
Try it yourself
Numbered positions, starting at 0
arrChoose an operationWatch the index numbers when you insert or delete in the middle. Every element after the one you touched gets a new index, and that renumbering is the O(n) in the table above.
When should you use an array?
Reach for an array when:
- The items belong together.
- You need quick access by position.
- The order of the items matters.
Everyday examples: the days of a week, a student's marks, daily temperatures, a playlist of songs.
Advantages
- Instant lookups: Jump straight to any element using its index.
- Hardware friendly: CPUs love reading continuous memory; it's extremely fast to cache.
- The foundation: Advanced structures like matrices, hash tables, and heaps are all built on top of arrays.
Where arrays already are
- Strings → stored as arrays of characters.
- Images → pixels stored in arrays, row by row.
- Buffers → audio samples and network packets land in arrays before anything reads them.
Key takeaway
An array is a neatly organised row of numbered lockers: finding an item is instant, but inserting or removing one in the middle means shifting everything after it.