Introduction to Arrays
Arrays are one of the most fundamental data structures in computer science. They serve as the building blocks for many other data structures.
Ideally, what is an Array?
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together.
The Problem with Variables
Imagine you need to store the marks of 5 students. Simple enough, just create 5 variables:
int mark1 = 90;
int mark2 = 85;
int mark3 = 78;
int mark4 = 92;
int mark5 = 88;But what if you need to store the marks for:
- An entire class (50 students)?
- An entire school (1000 students)?
- An entire university (20,000 students)?
Creating 20,000 individual variables is impossible to manage! You can’t loop through mark1, mark2… mark20000.
The Array Solution
This is where Arrays come in. An array allows you to store all these values in a single variable structure, accessible by an index.
Think of an array as a row of lockers in a school hallway. Each locker has a unique number (index) and can hold one item (student’s marks).
Instead of mark1, mark2, etc., we have marks[0], marks[1], marks[2], and so on.
Key Characteristics
- Fixed Size: In many languages (like C and Java), the size of the array must be known at compile time.
- Same Data Type: You can’t store an integer in one slot and a string in another (in strictly typed languages).
- Indexed Access: You can access any element instantly if you know its index.
- Contiguous Memory: Elements live next to each other in memory — this is what makes arrays fast.
Types of Arrays
By Dimensions
| Type | Description | Example |
|---|---|---|
| 1D Array | A single row of elements | [1, 2, 3, 4, 5] |
| 2D Array | A table/matrix of elements | [[1,2], [3,4], [5,6]] |
| 3D+ Array | Multi-dimensional grids | Used in image processing, scientific computing |
By Size Behavior
| Type | Languages | Resizable? |
|---|---|---|
| Static Array | C, C++ (int arr[5]), Java | No — size fixed at creation |
| Dynamic Array | Python (list), JavaScript, C++ (vector) | Yes — grows/shrinks as needed |
Dynamic arrays aren’t magic. When a dynamic array runs out of space, it allocates a new, larger block of memory (usually 2x), copies everything over, and frees the old block. This resize operation costs O(n), but it happens rarely enough that insertions are still O(1) amortized.
Where Are Arrays Used?
Arrays are everywhere. Here are some places you’ve already used them without realizing:
- Storing pixel data — every image is a 2D array of pixel values
- Strings — in C, a string is just an array of characters ending with
\0 - Lookup tables — mapping an index to a value in O(1)
- Stacks and Queues — often implemented using arrays under the hood
- Sorting and Searching — almost every algorithm operates on arrays
- Database records — rows of data stored in contiguous blocks
Try It Yourself
Play with this interactive array. Insert, delete, and search for elements to build intuition for how arrays work: