Day 2 - Arrays and StringsArraysIntroduction

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, mark2mark20000.

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.

marks
90
[0]
85
[1]
78
[2]
92
[3]
88
[4]

Key Characteristics

  1. Fixed Size: In many languages (like C and Java), the size of the array must be known at compile time.
  2. Same Data Type: You can’t store an integer in one slot and a string in another (in strictly typed languages).
  3. Indexed Access: You can access any element instantly if you know its index.
  4. Contiguous Memory: Elements live next to each other in memory — this is what makes arrays fast.

Types of Arrays

By Dimensions

TypeDescriptionExample
1D ArrayA single row of elements[1, 2, 3, 4, 5]
2D ArrayA table/matrix of elements[[1,2], [3,4], [5,6]]
3D+ ArrayMulti-dimensional gridsUsed in image processing, scientific computing

By Size Behavior

TypeLanguagesResizable?
Static ArrayC, C++ (int arr[5]), JavaNo — size fixed at creation
Dynamic ArrayPython (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:

Try it: Interactive Array
10
[0]
20
[1]
30
[2]
40
[3]
50
[4]

Quick Check

What is the index of the first element in an array?
What does 'contiguous memory' mean for arrays?