Understanding Arrays
Arrays are one of the most fundamental data structures in programming. They store a collection of elements in a fixed order, allowing you to access each item by its index.
Key Concepts
- Zero‑based indexing: The first element is at index
0. - Homogeneous vs. heterogeneous: Some languages enforce a single type per array (e.g., Java), while others allow mixed types (e.g., JavaScript).
- Fixed vs. dynamic size: Traditional arrays have a fixed length; dynamic arrays (lists, vectors) can grow.
Array Syntax in Different Languages
// JavaScript
const nums = [1, 2, 3, 4];
console.log(nums[0]); // 1
# Python
nums = [1, 2, 3, 4]
print(nums[0]) # 1
// Java
int[] nums = {1, 2, 3, 4};
System.out.println(nums[0]); // 1