Array
Like a weekly pill organizer with labeled compartments, it stores items in equal-sized slots placed side by side in numbered order.
Definition The most fundamental storage structure in computer memory that holds items of the same type seamlessly in a single row. Because each slot has a numbered label (an index), you can instantly grab any item you want without searching through the rest.
Filling a Compartment Pill Organizer
Imagine a weekly pillbox labeled Monday through Sunday. Each compartment holds specific pills, and the slots are attached snugly without any gaps in between.
Computers frequently store data using this exact system. This is an array. When you create an array, the computer lines up identical storage slots contiguously in memory. Because the slots are glued together side by side, managing the data stays remarkably neat and orderly.
What is interesting is that computer index labels usually start at 0 instead of 1. The first slot is 0, and the second is 1. While we count from 1 in everyday life, computers measure "how far you are from the starting line," meaning the starting point itself sits at an offset of 0.
The Secret to Instant Lookup by Number
If items are scattered randomly across a house, you have to search everywhere to find what you need. But what if you have a neat row of identical drawers? As long as you know the drawer number, you can reach right into it with your eyes closed.
The greatest superpower of an array is its lightning-fast lookup speed. As long as the computer knows the starting address and the size of a single slot, it calculates any slot's location instantly with basic multiplication. To find slot number 5, it simply takes the starting address and adds (slot size ร 5) in a single leap.
Whether you have 10 items or 1 million items, jumping to a specific numbered slot takes the exact same amount of time. Computer science calls this jumping directly without scanning through, or "random access."
The Catch: Squeezing into the Middle Is Hard
However, arrays come with a clear drawback. What happens if you try to force a new pill between Wednesday and Thursday in a full pillbox? You have to push every pill from Thursday through Sunday one slot to the right to make room.
Arrays work the same way. To insert a new item at the beginning or in the middle, you must shift every following item back by one spot. If you remove an item, you have to slide all remaining items forward to fill the gap. That is why inserting or deleting data can be slow and costly.
Another limitation is that resizing the array on the fly is tricky. When a box fills up, you must create a brand-new, larger box and copy everything over. Because of this, programs often use different storage methods (such as linked lists) when data sizes change frequently or items are often inserted in the middle.
๐ค Common misconceptions
The first slot of an array is always numbered 1.
In most programming languages, the first slot index is 0. This is because the index represents an offsetโthe distance from the starting address.
๐งบ Where you meet it
An array is a foundational data structure that packs items side by side in memory, letting you retrieve any item in an instant using its index.