Indexes & Positions

LESSON CONTENT

Indexes & Positions

Overview

Indexing is the process of accessing a specific position inside an array. Each element is stored at a numbered position called an index. This index lets you read or modify that element directly.

Zero-Based Indexing

Most programming languages use zero-based indexing. This means the first element is at index 0, the second at index 1, and so on. If an array contains n elements, the valid index range is 0 to n − 1.

Example

Given the array:

[12, 45, 77, 30]

Indexes map to values like this:

  • 0 → 12
  • 1 → 45
  • 2 → 77
  • 3 → 30

Why Indexing Is Fast

Arrays store elements in contiguous memory. Because of this, the computer can calculate the exact location of any element instantly, making indexing an O(1) constant-time operation.

Out-of-Range Indexes

If you attempt to access an index outside the valid range, the program will throw an error. Always make sure the index is within the array’s bounds.