What is an Array?
You'll learn: What arrays are and how they organize multiple values of the same type in a contiguous block of memory.
The Core Idea
An array is a data structure that stores a fixed number of elements, all of the same type, in a single contiguous block of memory. Think of it like a row of mailboxes in an apartment building—each mailbox has a numbered address (starting from 0), and each one holds the same kind of thing (letters). Once you build the row, the number of mailboxes is fixed.
Why Contiguous Memory Matters
Arrays allocate memory in one continuous chunk. If you create an array of 5 integers, the computer reserves space for exactly 5 integers sitting right next to each other in memory. This arrangement makes arrays extremely fast for accessing elements—if you know where the array starts and which position you want, the computer can calculate the exact memory address instantly.
Key Characteristics
- Fixed size: When you declare an array, you specify how many elements it holds. This size cannot change.
- Same type: Every element must be the same data type (all integers, all floats, all characters, etc.).
- Indexed access: Each element has a numeric position, typically starting at index 0.
- Fast lookups: Getting the value at any position takes the same constant amount of time.
A Simple Analogy
Imagine a parking lot with 10 numbered spaces (0 through 9). Each space can hold exactly one car (same type), the spaces are right next to each other (contiguous), and you can't add an 11th space without building a new lot (fixed size).
Key Takeaway: Arrays store a fixed number of same-type elements in contiguous memory, enabling fast, indexed access to any element.