Published by Patrick Mutisya · 14 days ago
An array is a collection of elements of the same data type stored in contiguous memory locations and accessed by a numeric index (or subscript).
new in Java).for loop.Java (static, zero‑based):
int[] scores = new int[5]; // declaration, length 5
int[] ages = {12, 15, 17, 19, 21}; // declaration + initialization
Python (dynamic, zero‑based):
scores = [0] * 5 # list of length 5
ages = [12, 15, 17, 19, 21]
When an array is stored in contiguous memory, the address of element i can be computed as:
\$\$
\text{address}(i) = \text{baseAddress} + i \times \text{elementSize}
\$\$
where i is the zero‑based index.
For an array A with R rows and C columns, the address of element A[i][j] is:
\$\$
\text{address}(i,j) = \text{baseAddress} + \bigl(i \times C + j\bigr) \times \text{elementSize}
\$\$
In column‑major order the formula swaps the roles of i and j.
value = array[index];array[index] = new \cdot alue;0 ≤ index < length.| Term | Definition |
|---|---|
| Array | Ordered collection of same‑type elements stored contiguously. |
| Element | Individual value stored at a particular index. |
| Index / Subscript | Integer used to locate an element; first index is usually 0. |
| Bounds | Lowest and highest valid indices (0 and length‑1 for zero‑based arrays). |
| Length / Size | Number of elements an array can contain. |
| Dimension | Number of indices required (1‑D, 2‑D, …). |
| Static Array | Size fixed at compile time. |
| Dynamic Array | Size determined or changed at run time. |
| Row‑major order | Rows stored consecutively in memory. |
| Column‑major order | Columns stored consecutively in memory. |
| Bounds checking | Runtime test that an index is within valid limits. |
| Out‑of‑bounds error | Exception or undefined behaviour caused by an illegal index. |