数组是内存中的连续块,用于存储相同类型的元素,从 0 开始索引。由于元素彼此相邻,第 i 个元素的地址可直接计算为 base + i * elementSize,从而实现 O(1) 随机访问。
Memory layout
text
index: 0 1 2 3 4
+-----+-----+-----+-----+-----+
arr = | 10 | 20 | 30 | 40 | 50 |
+-----+-----+-----+-----+-----+
address: base +4 +8 +12 +16 (4-byte ints)
Example
python
arr = [, , , , ]
x = arr[]
arr.append()
arr.insert(, )
arr.pop()
