Array

An array is a collection of elements stored in contiguous memory locations and identified by at least one array index.

Features of Arrays: 1. Homogeneity: All elements are the same type. 2. Fixed Size: Once allocated, standard arrays cannot change size. 3. Zero-Indexing: Most modern languages start counting at 0. 4. Multi-dimensionality: Arrays can store other arrays (matrices).

        graph LR
  Center["Array"]:::main
  classDef main fill:#7c3aed,stroke:#8b5cf6,stroke-width:2px,color:white,font-weight:bold,rx:5,ry:5;
  classDef pre fill:#0f172a,stroke:#3b82f6,color:#94a3b8,rx:5,ry:5;
  classDef child fill:#0f172a,stroke:#10b981,color:#94a3b8,rx:5,ry:5;
  classDef related fill:#0f172a,stroke:#8b5cf6,stroke-dasharray: 5 5,color:#94a3b8,rx:5,ry:5;
  linkStyle default stroke:#4b5563,stroke-width:2px;

      

🧒 Explain Like I'm 5

Think of an [array](/en/terms/array) like a row of lockers in a school hallway. Each locker is the same size and has a number (the index). If you know the number, you can go straight to that locker and see what's inside without having to check all the others first.

🤓 Expert Deep Dive

At the hardware level, an array is a block of contiguous memory. The address of an element A[i] is calculated as BaseAddress + i * ElementSize. This hardware-level alignment is what enables [CPU cache](/en/terms/cpu-cache) optimizations like prefetching. However, this same contiguousness makes insertions and deletions expensive (O(n)), as subsequent elements must be moved to maintain the order. For large-scale data, the tradeoff between O(1) access and O(n) modification is a central design decision.

📚 Sources