Imagine you’re a teacher tasked with recording the math scores of 5 students. Without using an array, you might do something like this: score1 = 90, score2 = 85, score3 = 95, … But if there are 100 students, you’d have to write 100 such variables—this is not only tedious but also inefficient when you need to search or modify all scores one by one.

This is where arrays come in! An array is like an “ordered container” that packages all students’ scores into a single unit, making it easy to access and manage the data.

I. What Exactly Is an Array?

In simple terms, an array is a collection of elements of the same type, arranged in a specific order, where each element is accessed via an index (i.e., a position number).

For example, the scores of the 5 students above can be represented as:
scores = [90, 85, 95, 78, 92]

Here:
- scores is the array name (variable name);
- Elements inside the square brackets [] are the array’s “elements” (all numbers here, same type);
- Each element has a “position number” called an “index,” starting from 0.

II. Core Structure of Arrays: Indexes and Access

The most critical feature of arrays is that indexes start at 0. The first element is at position 0, the second at 1, the third at 2, and so on.

1. Declaration and Initialization

Syntax for declaring arrays varies slightly across programming languages, but the core idea is the same. Using Python as an example:

# Initialize an array with known elements
scores = [90, 85, 95, 78, 92]  # Array of length 5
scores = [0] * 5               # Array of length 5 with all elements as 0

2. Accessing Elements

To get an element from the array, use the syntax: array_name[index]. For example:

print(scores[0])  # Output: 90 (first element)
print(scores[3])  # Output: 78 (fourth element)

Note: If the index exceeds the array length (e.g., accessing scores[5] when the array has only 5 elements), an error will occur (Python will raise IndexError). Thus, indexes must be in the range 0 to length-1.

III. Basic Array Operations

1. Traversing the Array

“Traversal” means accessing all elements in order. This can be easily done with a loop:

for score in scores:
    print(score)  # Prints 90, 85, 95, 78, 92 in sequence

2. Inserting Elements

Suppose we want to insert a new score (e.g., 80 for the 6th student) at index 2 (third position). The steps are:
- Shift elements at index 2 and beyond one position to the right (e.g., 95 → index 3, 78 → index 4, 92 → index 5);
- Place the new score (80) at index 2.

After insertion, the array becomes: [90, 85, 80, 95, 78, 92].

3. Deleting Elements

To delete the element at index 2 (e.g., 80):
- Shift elements at index 3 and beyond one position to the left (e.g., 95 → index 2, 78 → index 3, 92 → index 4).

After deletion, the array reverts to: [90, 85, 95, 78, 92].

IV. Characteristics, Advantages, and Disadvantages

1. Key Characteristics

  • Same Type: All elements in an array must be of the same data type (e.g., all integers, all strings; mixing types is not allowed unless the language supports dynamic typing, like Python lists).
  • Index Starts at 0: The first element is at position 0, which simplifies address calculations.
  • Contiguous Storage: Elements are stored contiguously in memory, enabling direct access via index with extremely fast retrieval.

2. Pros and Cons

  • Advantages: Fast access (O(1) time complexity via index);
  • Disadvantages: Slow insertion/deletion (requires shifting elements, O(n) time complexity where n is the array length); Fixed size for static arrays (cannot dynamically expand).

V. Why Learn About Arrays?

Arrays are the “foundation” of data structures. You might think, “Arrays are simple—what’s the big deal?” However, many advanced structures (e.g., linked lists, stacks, queues, hash tables) are built on array concepts:
- Stacks can be implemented with arrays to enforce “last-in-first-out” behavior;
- Hash tables often rely on array indexing for storage.

Understanding the core idea of arrays (“ordered collection with fast index-based access, contiguous storage”) will help you analyze and design more complex data structures.

VI. Summary

An array is an ordered collection of elements of the same type, accessed via 0-based indexing, with elements stored contiguously in memory. Its strengths lie in fast access, while weaknesses include cumbersome insertion/deletion. Though simple, arrays are the first step in mastering data structures. Mastering arrays is your first step toward learning advanced data structures!

Xiaoye