Skip to content

4.1   Array

An array is a linear data structure that operates as a lineup of similar items, stored together in a computer's memory in contiguous spaces. It's like a sequence that maintains organized storage. Each item in this lineup has its unique 'spot' known as an index. Please refer to Figure 4-1 to observe how arrays work and grasp these key terms.

Array definition and storage method

Figure 4-1   Array definition and storage method

4.1.1   Common operations on arrays

1.   Initializing arrays

Arrays can be initialized in two ways depending on the needs: either without initial values or with specified initial values. When initial values are not specified, most programming languages will set the array elements to \(0\):

array.py
# Initialize array
arr: list[int] = [0] * 5  # [ 0, 0, 0, 0, 0 ]
nums: list[int] = [1, 3, 2, 5, 4]
array.cpp
/* Initialize array */
// Stored on stack
int arr[5];
int nums[5] = { 1, 3, 2, 5, 4 };
// Stored on heap (manual memory release needed)
int* arr1 = new int[5];
int* nums1 = new int[5] { 1, 3, 2, 5, 4 };
array.java
/* Initialize array */
int[] arr = new int[5]; // { 0, 0, 0, 0, 0 }
int[] nums = { 1, 3, 2, 5, 4 };
array.cs
/* Initialize array */
int[] arr = new int[5]; // [ 0, 0, 0, 0, 0 ]
int[] nums = [1, 3, 2, 5, 4];
array.go
/* Initialize array */
var arr [5]int
// In Go, specifying the length ([5]int) denotes an array, while not specifying it ([]int) denotes a slice.
// Since Go's arrays are designed to have compile-time fixed length, only constants can be used to specify the length.
// For convenience in implementing the extend() method, the Slice will be considered as an Array here.
nums := []int{1, 3, 2, 5, 4}
array.swift
/* Initialize array */
let arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]
let nums = [1, 3, 2, 5, 4]
array.js
/* Initialize array */
var arr = new Array(5).fill(0);
var nums = [1, 3, 2, 5, 4];
array.ts
/* Initialize array */
let arr: number[] = new Array(5).fill(0);
let nums: number[] = [1, 3, 2, 5, 4];
array.dart
/* Initialize array */
List<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]
List<int> nums = [1, 3, 2, 5, 4];
array.rs
/* Initialize array */
let arr: Vec<i32> = vec![0; 5]; // [0, 0, 0, 0, 0]
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
array.c
/* Initialize array */
int arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }
int nums[5] = { 1, 3, 2, 5, 4 };
array.kt

array.zig
// Initialize array
var arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }
var nums = [_]i32{ 1, 3, 2, 5, 4 };

2.   Accessing elements

Elements in an array are stored in contiguous memory spaces, making it simpler to compute each element's memory address. The formula shown in the Figure below aids in determining an element's memory address, utilizing the array's memory address (specifically, the first element's address) and the element's index. This computation streamlines direct access to the desired element.

Memory address calculation for array elements

Figure 4-2   Memory address calculation for array elements

As observed in the above illustration, array indexing conventionally begins at \(0\). While this might appear counterintuitive, considering counting usually starts at \(1\), within the address calculation formula, an index is essentially an offset from the memory address. For the first element's address, this offset is \(0\), validating its index as \(0\).

Accessing elements in an array is highly efficient, allowing us to randomly access any element in \(O(1)\) time.

array.py
def random_access(nums: list[int]) -> int:
    """随机访问元素"""
    # 在区间 [0, len(nums)-1] 中随机抽取一个数字
    random_index = random.randint(0, len(nums) - 1)
    # 获取并返回随机元素
    random_num = nums[random_index]
    return random_num
array.cpp
/* 随机访问元素 */
int randomAccess(int *nums, int size) {
    // 在区间 [0, size) 中随机抽取一个数字
    int randomIndex = rand() % size;
    // 获取并返回随机元素
    int randomNum = nums[randomIndex];
    return randomNum;
}
array.java
/* 随机访问元素 */
int randomAccess(int[] nums) {
    // 在区间 [0, nums.length) 中随机抽取一个数字
    int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
    // 获取并返回随机元素
    int randomNum = nums[randomIndex];
    return randomNum;
}
array.cs
/* 随机访问元素 */
int RandomAccess(int[] nums) {
    Random random = new();
    // 在区间 [0, nums.Length) 中随机抽取一个数字
    int randomIndex = random.Next(nums.Length);
    // 获取并返回随机元素
    int randomNum = nums[randomIndex];
    return randomNum;
}
array.go
/* 随机访问元素 */
func randomAccess(nums []int) (randomNum int) {
    // 在区间 [0, nums.length) 中随机抽取一个数字
    randomIndex := rand.Intn(len(nums))
    // 获取并返回随机元素
    randomNum = nums[randomIndex]
    return
}
array.swift
/* 随机访问元素 */
func randomAccess(nums: [Int]) -> Int {
    // 在区间 [0, nums.count) 中随机抽取一个数字
    let randomIndex = nums.indices.randomElement()!
    // 获取并返回随机元素
    let randomNum = nums[randomIndex]
    return randomNum
}
array.js
/* 随机访问元素 */
function randomAccess(nums) {
    // 在区间 [0, nums.length) 中随机抽取一个数字
    const random_index = Math.floor(Math.random() * nums.length);
    // 获取并返回随机元素
    const random_num = nums[random_index];
    return random_num;
}
array.ts
/* 随机访问元素 */
function randomAccess(nums: number[]): number {
    // 在区间 [0, nums.length) 中随机抽取一个数字
    const random_index = Math.floor(Math.random() * nums.length);
    // 获取并返回随机元素
    const random_num = nums[random_index];
    return random_num;
}
array.dart
/* 随机访问元素 */
int randomAccess(List<int> nums) {
  // 在区间 [0, nums.length) 中随机抽取一个数字
  int randomIndex = Random().nextInt(nums.length);
  // 获取并返回随机元素
  int randomNum = nums[randomIndex];
  return randomNum;
}
array.rs
/* 随机访问元素 */
fn random_access(nums: &[i32]) -> i32 {
    // 在区间 [0, nums.len()) 中随机抽取一个数字
    let random_index = rand::thread_rng().gen_range(0..nums.len());
    // 获取并返回随机元素
    let random_num = nums[random_index];
    random_num
}
array.c
/* 随机访问元素 */
int randomAccess(int *nums, int size) {
    // 在区间 [0, size) 中随机抽取一个数字
    int randomIndex = rand() % size;
    // 获取并返回随机元素
    int randomNum = nums[randomIndex];
    return randomNum;
}
array.kt
/* 随机访问元素 */
fun randomAccess(nums: IntArray): Int {
    // 在区间 [0, nums.size) 中随机抽取一个数字
    val randomIndex = ThreadLocalRandom.current().nextInt(0, nums.size)
    // 获取并返回随机元素
    val randomNum = nums[randomIndex]
    return randomNum
}
array.rb
### 随机访问元素 ###
def random_access(nums)
  # 在区间 [0, nums.length) 中随机抽取一个数字
  random_index = Random.rand(0...nums.length)

  # 获取并返回随机元素
  nums[random_index]
end
array.zig
// 随机访问元素
fn randomAccess(nums: []i32) i32 {
    // 在区间 [0, nums.len) 中随机抽取一个整数
    var randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);
    // 获取并返回随机元素
    var randomNum = nums[randomIndex];
    return randomNum;
}
Code Visualization

3.   Inserting elements

Array elements are tightly packed in memory, with no space available to accommodate additional data between them. Illustrated in Figure below, inserting an element in the middle of an array requires shifting all subsequent elements back by one position to create room for the new element.

Array element insertion example

Figure 4-3   Array element insertion example

It's important to note that due to the fixed length of an array, inserting an element will unavoidably result in the loss of the last element in the array. Solutions to address this issue will be explored in the "List" chapter.

array.py
def insert(nums: list[int], num: int, index: int):
    """在数组的索引 index 处插入元素 num"""
    # 把索引 index 以及之后的所有元素向后移动一位
    for i in range(len(nums) - 1, index, -1):
        nums[i] = nums[i - 1]
    # 将 num 赋给 index 处的元素
    nums[index] = num
array.cpp
/* 在数组的索引 index 处插入元素 num */
void insert(int *nums, int size, int num, int index) {
    // 把索引 index 以及之后的所有元素向后移动一位
    for (int i = size - 1; i > index; i--) {
        nums[i] = nums[i - 1];
    }
    // 将 num 赋给 index 处的元素
    nums[index] = num;
}
array.java
/* 在数组的索引 index 处插入元素 num */
void insert(int[] nums, int num, int index) {
    // 把索引 index 以及之后的所有元素向后移动一位
    for (int i = nums.length - 1; i > index; i--) {
        nums[i] = nums[i - 1];
    }
    // 将 num 赋给 index 处的元素
    nums[index] = num;
}
array.cs
/* 在数组的索引 index 处插入元素 num */
void Insert(int[] nums, int num, int index) {
    // 把索引 index 以及之后的所有元素向后移动一位
    for (int i = nums.Length - 1; i > index; i--) {
        nums[i] = nums[i - 1];
    }
    // 将 num 赋给 index 处的元素
    nums[index] = num;
}
array.go
/* 在数组的索引 index 处插入元素 num */
func insert(nums []int, num int, index int) {
    // 把索引 index 以及之后的所有元素向后移动一位
    for i := len(nums) - 1; i > index; i-- {
        nums[i] = nums[i-1]
    }
    // 将 num 赋给 index 处的元素
    nums[index] = num
}
array.swift
/* 在数组的索引 index 处插入元素 num */
func insert(nums: inout [Int], num: Int, index: Int) {
    // 把索引 index 以及之后的所有元素向后移动一位
    for i in nums.indices.dropFirst(index).reversed() {
        nums[i] = nums[i - 1]
    }
    // 将 num 赋给 index 处的元素
    nums[index] = num
}
array.js
/* 在数组的索引 index 处插入元素 num */
function insert(nums, num, index) {
    // 把索引 index 以及之后的所有元素向后移动一位
    for (let i = nums.length - 1; i > index; i--) {
        nums[i] = nums[i - 1];
    }
    // 将 num 赋给 index 处的元素
    nums[index] = num;
}
array.ts
/* 在数组的索引 index 处插入元素 num */
function insert(nums: number[], num: number, index: number): void {
    // 把索引 index 以及之后的所有元素向后移动一位
    for (let i = nums.length - 1; i > index; i--) {
        nums[i] = nums[i - 1];
    }
    // 将 num 赋给 index 处的元素
    nums[index] = num;
}
array.dart
/* 在数组的索引 index 处插入元素 _num */
void insert(List<int> nums, int _num, int index) {
  // 把索引 index 以及之后的所有元素向后移动一位
  for (var i = nums.length - 1; i > index; i--) {
    nums[i] = nums[i - 1];
  }
  // 将 _num 赋给 index 处元素
  nums[index] = _num;
}
array.rs
/* 在数组的索引 index 处插入元素 num */
fn insert(nums: &mut Vec<i32>, num: i32, index: usize) {
    // 把索引 index 以及之后的所有元素向后移动一位
    for i in (index + 1..nums.len()).rev() {
        nums[i] = nums[i - 1];
    }
    // 将 num 赋给 index 处的元素
    nums[index] = num;
}
array.c
/* 在数组的索引 index 处插入元素 num */
void insert(int *nums, int size, int num, int index) {
    // 把索引 index 以及之后的所有元素向后移动一位
    for (int i = size - 1; i > index; i--) {
        nums[i] = nums[i - 1];
    }
    // 将 num 赋给 index 处的元素
    nums[index] = num;
}
array.kt
/* 在数组的索引 index 处插入元素 num */
fun insert(nums: IntArray, num: Int, index: Int) {
    // 把索引 index 以及之后的所有元素向后移动一位
    for (i in nums.size - 1 downTo index + 1) {
        nums[i] = nums[i - 1]
    }
    // 将 num 赋给 index 处的元素
    nums[index] = num
}
array.rb
### 在数组的索引 index 处插入元素 num ###
def insert(nums, num, index)
  # 把索引 index 以及之后的所有元素向后移动一位
  for i in (nums.length - 1).downto(index + 1)
    nums[i] = nums[i - 1]
  end

  # 将 num 赋给 index 处的元素
  nums[index] = num
end
array.zig
// 在数组的索引 index 处插入元素 num
fn insert(nums: []i32, num: i32, index: usize) void {
    // 把索引 index 以及之后的所有元素向后移动一位
    var i = nums.len - 1;
    while (i > index) : (i -= 1) {
        nums[i] = nums[i - 1];
    }
    // 将 num 赋给 index 处的元素
    nums[index] = num;
}
Code Visualization

4.   Deleting elements

Similarly, as depicted in Figure 4-4, to delete an element at index \(i\), all elements following index \(i\) must be moved forward by one position.

Array element deletion example

Figure 4-4   Array element deletion example

Please note that after deletion, the former last element becomes "meaningless," hence requiring no specific modification.

array.py
def remove(nums: list[int], index: int):
    """删除索引 index 处的元素"""
    # 把索引 index 之后的所有元素向前移动一位
    for i in range(index, len(nums) - 1):
        nums[i] = nums[i + 1]
array.cpp
/* 删除索引 index 处的元素 */
void remove(int *nums, int size, int index) {
    // 把索引 index 之后的所有元素向前移动一位
    for (int i = index; i < size - 1; i++) {
        nums[i] = nums[i + 1];
    }
}
array.java
/* 删除索引 index 处的元素 */
void remove(int[] nums, int index) {
    // 把索引 index 之后的所有元素向前移动一位
    for (int i = index; i < nums.length - 1; i++) {
        nums[i] = nums[i + 1];
    }
}
array.cs
/* 删除索引 index 处的元素 */
void Remove(int[] nums, int index) {
    // 把索引 index 之后的所有元素向前移动一位
    for (int i = index; i < nums.Length - 1; i++) {
        nums[i] = nums[i + 1];
    }
}
array.go
/* 删除索引 index 处的元素 */
func remove(nums []int, index int) {
    // 把索引 index 之后的所有元素向前移动一位
    for i := index; i < len(nums)-1; i++ {
        nums[i] = nums[i+1]
    }
}
array.swift
/* 删除索引 index 处的元素 */
func remove(nums: inout [Int], index: Int) {
    // 把索引 index 之后的所有元素向前移动一位
    for i in nums.indices.dropFirst(index).dropLast() {
        nums[i] = nums[i + 1]
    }
}
array.js
/* 删除索引 index 处的元素 */
function remove(nums, index) {
    // 把索引 index 之后的所有元素向前移动一位
    for (let i = index; i < nums.length - 1; i++) {
        nums[i] = nums[i + 1];
    }
}
array.ts
/* 删除索引 index 处的元素 */
function remove(nums: number[], index: number): void {
    // 把索引 index 之后的所有元素向前移动一位
    for (let i = index; i < nums.length - 1; i++) {
        nums[i] = nums[i + 1];
    }
}
array.dart
/* 删除索引 index 处的元素 */
void remove(List<int> nums, int index) {
  // 把索引 index 之后的所有元素向前移动一位
  for (var i = index; i < nums.length - 1; i++) {
    nums[i] = nums[i + 1];
  }
}
array.rs
/* 删除索引 index 处的元素 */
fn remove(nums: &mut Vec<i32>, index: usize) {
    // 把索引 index 之后的所有元素向前移动一位
    for i in index..nums.len() - 1 {
        nums[i] = nums[i + 1];
    }
}
array.c
/* 删除索引 index 处的元素 */
// 注意:stdio.h 占用了 remove 关键词
void removeItem(int *nums, int size, int index) {
    // 把索引 index 之后的所有元素向前移动一位
    for (int i = index; i < size - 1; i++) {
        nums[i] = nums[i + 1];
    }
}
array.kt
/* 删除索引 index 处的元素 */
fun remove(nums: IntArray, index: Int) {
    // 把索引 index 之后的所有元素向前移动一位
    for (i in index..<nums.size - 1) {
        nums[i] = nums[i + 1]
    }
}
array.rb
### 删除索引 index 处的元素 ###
def remove(nums, index)
  # 把索引 index 之后的所有元素向前移动一位
  for i in index...(nums.length - 1)
    nums[i] = nums[i + 1]
  end
end
array.zig
// 删除索引 index 处的元素
fn remove(nums: []i32, index: usize) void {
    // 把索引 index 之后的所有元素向前移动一位
    var i = index;
    while (i < nums.len - 1) : (i += 1) {
        nums[i] = nums[i + 1];
    }
}
Code Visualization

In summary, the insertion and deletion operations in arrays present the following disadvantages:

  • High time complexity: Both insertion and deletion in an array have an average time complexity of \(O(n)\), where \(n\) is the length of the array.
  • Loss of elements: Due to the fixed length of arrays, elements that exceed the array's capacity are lost during insertion.
  • Waste of memory: Initializing a longer array and utilizing only the front part results in "meaningless" end elements during insertion, leading to some wasted memory space.

5.   Traversing arrays

In most programming languages, we can traverse an array either by using indices or by directly iterating over each element:

array.py
def traverse(nums: list[int]):
    """遍历数组"""
    count = 0
    # 通过索引遍历数组
    for i in range(len(nums)):
        count += nums[i]
    # 直接遍历数组元素
    for num in nums:
        count += num
    # 同时遍历数据索引和元素
    for i, num in enumerate(nums):
        count += nums[i]
        count += num
array.cpp
/* 遍历数组 */
void traverse(int *nums, int size) {
    int count = 0;
    // 通过索引遍历数组
    for (int i = 0; i < size; i++) {
        count += nums[i];
    }
}
array.java
/* 遍历数组 */
void traverse(int[] nums) {
    int count = 0;
    // 通过索引遍历数组
    for (int i = 0; i < nums.length; i++) {
        count += nums[i];
    }
    // 直接遍历数组元素
    for (int num : nums) {
        count += num;
    }
}
array.cs
/* 遍历数组 */
void Traverse(int[] nums) {
    int count = 0;
    // 通过索引遍历数组
    for (int i = 0; i < nums.Length; i++) {
        count += nums[i];
    }
    // 直接遍历数组元素
    foreach (int num in nums) {
        count += num;
    }
}
array.go
/* 遍历数组 */
func traverse(nums []int) {
    count := 0
    // 通过索引遍历数组
    for i := 0; i < len(nums); i++ {
        count += nums[i]
    }
    count = 0
    // 直接遍历数组元素
    for _, num := range nums {
        count += num
    }
    // 同时遍历数据索引和元素
    for i, num := range nums {
        count += nums[i]
        count += num
    }
}
array.swift
/* 遍历数组 */
func traverse(nums: [Int]) {
    var count = 0
    // 通过索引遍历数组
    for i in nums.indices {
        count += nums[i]
    }
    // 直接遍历数组元素
    for num in nums {
        count += num
    }
    // 同时遍历数据索引和元素
    for (i, num) in nums.enumerated() {
        count += nums[i]
        count += num
    }
}
array.js
/* 遍历数组 */
function traverse(nums) {
    let count = 0;
    // 通过索引遍历数组
    for (let i = 0; i < nums.length; i++) {
        count += nums[i];
    }
    // 直接遍历数组元素
    for (const num of nums) {
        count += num;
    }
}
array.ts
/* 遍历数组 */
function traverse(nums: number[]): void {
    let count = 0;
    // 通过索引遍历数组
    for (let i = 0; i < nums.length; i++) {
        count += nums[i];
    }
    // 直接遍历数组元素
    for (const num of nums) {
        count += num;
    }
}
array.dart
/* 遍历数组元素 */
void traverse(List<int> nums) {
  int count = 0;
  // 通过索引遍历数组
  for (var i = 0; i < nums.length; i++) {
    count += nums[i];
  }
  // 直接遍历数组元素
  for (int _num in nums) {
    count += _num;
  }
  // 通过 forEach 方法遍历数组
  nums.forEach((_num) {
    count += _num;
  });
}
array.rs
/* 遍历数组 */
fn traverse(nums: &[i32]) {
    let mut _count = 0;
    // 通过索引遍历数组
    for i in 0..nums.len() {
        _count += nums[i];
    }
    // 直接遍历数组元素
    for num in nums {
        _count += num;
    }
}
array.c
/* 遍历数组 */
void traverse(int *nums, int size) {
    int count = 0;
    // 通过索引遍历数组
    for (int i = 0; i < size; i++) {
        count += nums[i];
    }
}
array.kt
/* 遍历数组 */
fun traverse(nums: IntArray) {
    var count = 0
    // 通过索引遍历数组
    for (i in nums.indices) {
        count += nums[i]
    }
    // 直接遍历数组元素
    for (j in nums) {
        count += j
    }
}
array.rb
### 遍历数组 ###
def traverse(nums)
  count = 0

  # 通过索引遍历数组
  for i in 0...nums.length
    count += nums[i]
  end

  # 直接遍历数组元素
  for num in nums
    count += num
  end
end
array.zig
// 遍历数组
fn traverse(nums: []i32) void {
    var count: i32 = 0;
    // 通过索引遍历数组
    var i: i32 = 0;
    while (i < nums.len) : (i += 1) {
        count += nums[i];
    }
    count = 0;
    // 直接遍历数组元素
    for (nums) |num| {
        count += num;
    }
}
Code Visualization

6.   Finding elements

Locating a specific element within an array involves iterating through the array, checking each element to determine if it matches the desired value.

Because arrays are linear data structures, this operation is commonly referred to as "linear search."

array.py
def find(nums: list[int], target: int) -> int:
    """在数组中查找指定元素"""
    for i in range(len(nums)):
        if nums[i] == target:
            return i
    return -1
array.cpp
/* 在数组中查找指定元素 */
int find(int *nums, int size, int target) {
    for (int i = 0; i < size; i++) {
        if (nums[i] == target)
            return i;
    }
    return -1;
}
array.java
/* 在数组中查找指定元素 */
int find(int[] nums, int target) {
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == target)
            return i;
    }
    return -1;
}
array.cs
/* 在数组中查找指定元素 */
int Find(int[] nums, int target) {
    for (int i = 0; i < nums.Length; i++) {
        if (nums[i] == target)
            return i;
    }
    return -1;
}
array.go
/* 在数组中查找指定元素 */
func find(nums []int, target int) (index int) {
    index = -1
    for i := 0; i < len(nums); i++ {
        if nums[i] == target {
            index = i
            break
        }
    }
    return
}
array.swift
/* 在数组中查找指定元素 */
func find(nums: [Int], target: Int) -> Int {
    for i in nums.indices {
        if nums[i] == target {
            return i
        }
    }
    return -1
}
array.js
/* 在数组中查找指定元素 */
function find(nums, target) {
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] === target) return i;
    }
    return -1;
}
array.ts
/* 在数组中查找指定元素 */
function find(nums: number[], target: number): number {
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] === target) {
            return i;
        }
    }
    return -1;
}
array.dart
/* 在数组中查找指定元素 */
int find(List<int> nums, int target) {
  for (var i = 0; i < nums.length; i++) {
    if (nums[i] == target) return i;
  }
  return -1;
}
array.rs
/* 在数组中查找指定元素 */
fn find(nums: &[i32], target: i32) -> Option<usize> {
    for i in 0..nums.len() {
        if nums[i] == target {
            return Some(i);
        }
    }
    None
}
array.c
/* 在数组中查找指定元素 */
int find(int *nums, int size, int target) {
    for (int i = 0; i < size; i++) {
        if (nums[i] == target)
            return i;
    }
    return -1;
}
array.kt
/* 在数组中查找指定元素 */
fun find(nums: IntArray, target: Int): Int {
    for (i in nums.indices) {
        if (nums[i] == target)
            return i
    }
    return -1
}
array.rb
### 在数组中查找指定元素 ###
def find(nums, target)
  for i in 0...nums.length
    return i if nums[i] == target
  end

  -1
end
array.zig
// 在数组中查找指定元素
fn find(nums: []i32, target: i32) i32 {
    for (nums, 0..) |num, i| {
        if (num == target) return @intCast(i);
    }
    return -1;
}
Code Visualization

7.   Expanding arrays

In complex system environments, ensuring the availability of memory space after an array for safe capacity extension becomes challenging. Consequently, in most programming languages, the length of an array is immutable.

To expand an array, it's necessary to create a larger array and then copy the elements from the original array. This operation has a time complexity of \(O(n)\) and can be time-consuming for large arrays. The code are as follows:

array.py
def extend(nums: list[int], enlarge: int) -> list[int]:
    """扩展数组长度"""
    # 初始化一个扩展长度后的数组
    res = [0] * (len(nums) + enlarge)
    # 将原数组中的所有元素复制到新数组
    for i in range(len(nums)):
        res[i] = nums[i]
    # 返回扩展后的新数组
    return res
array.cpp
/* 扩展数组长度 */
int *extend(int *nums, int size, int enlarge) {
    // 初始化一个扩展长度后的数组
    int *res = new int[size + enlarge];
    // 将原数组中的所有元素复制到新数组
    for (int i = 0; i < size; i++) {
        res[i] = nums[i];
    }
    // 释放内存
    delete[] nums;
    // 返回扩展后的新数组
    return res;
}
array.java
/* 扩展数组长度 */
int[] extend(int[] nums, int enlarge) {
    // 初始化一个扩展长度后的数组
    int[] res = new int[nums.length + enlarge];
    // 将原数组中的所有元素复制到新数组
    for (int i = 0; i < nums.length; i++) {
        res[i] = nums[i];
    }
    // 返回扩展后的新数组
    return res;
}
array.cs
/* 扩展数组长度 */
int[] Extend(int[] nums, int enlarge) {
    // 初始化一个扩展长度后的数组
    int[] res = new int[nums.Length + enlarge];
    // 将原数组中的所有元素复制到新数组
    for (int i = 0; i < nums.Length; i++) {
        res[i] = nums[i];
    }
    // 返回扩展后的新数组
    return res;
}
array.go
/* 扩展数组长度 */
func extend(nums []int, enlarge int) []int {
    // 初始化一个扩展长度后的数组
    res := make([]int, len(nums)+enlarge)
    // 将原数组中的所有元素复制到新数组
    for i, num := range nums {
        res[i] = num
    }
    // 返回扩展后的新数组
    return res
}
array.swift
/* 扩展数组长度 */
func extend(nums: [Int], enlarge: Int) -> [Int] {
    // 初始化一个扩展长度后的数组
    var res = Array(repeating: 0, count: nums.count + enlarge)
    // 将原数组中的所有元素复制到新数组
    for i in nums.indices {
        res[i] = nums[i]
    }
    // 返回扩展后的新数组
    return res
}
array.js
/* 扩展数组长度 */
// 请注意,JavaScript 的 Array 是动态数组,可以直接扩展
// 为了方便学习,本函数将 Array 看作长度不可变的数组
function extend(nums, enlarge) {
    // 初始化一个扩展长度后的数组
    const res = new Array(nums.length + enlarge).fill(0);
    // 将原数组中的所有元素复制到新数组
    for (let i = 0; i < nums.length; i++) {
        res[i] = nums[i];
    }
    // 返回扩展后的新数组
    return res;
}
array.ts
/* 扩展数组长度 */
// 请注意,TypeScript 的 Array 是动态数组,可以直接扩展
// 为了方便学习,本函数将 Array 看作长度不可变的数组
function extend(nums: number[], enlarge: number): number[] {
    // 初始化一个扩展长度后的数组
    const res = new Array(nums.length + enlarge).fill(0);
    // 将原数组中的所有元素复制到新数组
    for (let i = 0; i < nums.length; i++) {
        res[i] = nums[i];
    }
    // 返回扩展后的新数组
    return res;
}
array.dart
/* 扩展数组长度 */
List<int> extend(List<int> nums, int enlarge) {
  // 初始化一个扩展长度后的数组
  List<int> res = List.filled(nums.length + enlarge, 0);
  // 将原数组中的所有元素复制到新数组
  for (var i = 0; i < nums.length; i++) {
    res[i] = nums[i];
  }
  // 返回扩展后的新数组
  return res;
}
array.rs
/* 扩展数组长度 */
fn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {
    // 初始化一个扩展长度后的数组
    let mut res: Vec<i32> = vec![0; nums.len() + enlarge];
    // 将原数组中的所有元素复制到新
    for i in 0..nums.len() {
        res[i] = nums[i];
    }
    // 返回扩展后的新数组
    res
}
array.c
/* 扩展数组长度 */
int *extend(int *nums, int size, int enlarge) {
    // 初始化一个扩展长度后的数组
    int *res = (int *)malloc(sizeof(int) * (size + enlarge));
    // 将原数组中的所有元素复制到新数组
    for (int i = 0; i < size; i++) {
        res[i] = nums[i];
    }
    // 初始化扩展后的空间
    for (int i = size; i < size + enlarge; i++) {
        res[i] = 0;
    }
    // 返回扩展后的新数组
    return res;
}
array.kt
/* 扩展数组长度 */
fun extend(nums: IntArray, enlarge: Int): IntArray {
    // 初始化一个扩展长度后的数组
    val res = IntArray(nums.size + enlarge)
    // 将原数组中的所有元素复制到新数组
    for (i in nums.indices) {
        res[i] = nums[i]
    }
    // 返回扩展后的新数组
    return res
}
array.rb
### 扩展数组长度 ###
# 请注意,Ruby 的 Array 是动态数组,可以直接扩展
# 为了方便学习,本函数将 Array 看作长度不可变的数组
def extend(nums, enlarge)
  # 初始化一个扩展长度后的数组
  res = Array.new(nums.length + enlarge, 0)

  # 将原数组中的所有元素复制到新数组
  for i in 0...nums.length
    res[i] = nums[i]
  end

  # 返回扩展后的新数组
  res
end
array.zig
// 扩展数组长度
fn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {
    // 初始化一个扩展长度后的数组
    var res = try mem_allocator.alloc(i32, nums.len + enlarge);
    @memset(res, 0);
    // 将原数组中的所有元素复制到新数组
    std.mem.copy(i32, res, nums);
    // 返回扩展后的新数组
    return res;
}
Code Visualization

4.1.2   Advantages and limitations of arrays

Arrays are stored in contiguous memory spaces and consist of elements of the same type. This approach provides substantial prior information that systems can leverage to optimize the efficiency of data structure operations.

  • High space efficiency: Arrays allocate a contiguous block of memory for data, eliminating the need for additional structural overhead.
  • Support for random access: Arrays allow \(O(1)\) time access to any element.
  • Cache locality: When accessing array elements, the computer not only loads them but also caches the surrounding data, utilizing high-speed cache to enchance subsequent operation speeds.

However, continuous space storage is a double-edged sword, with the following limitations:

  • Low efficiency in insertion and deletion: As arrays accumulate many elements, inserting or deleting elements requires shifting a large number of elements.
  • Fixed length: The length of an array is fixed after initialization. Expanding an array requires copying all data to a new array, incurring significant costs.
  • Space wastage: If the allocated array size exceeds the what is necessary, the extra space is wasted.

4.1.3   Typical applications of arrays

Arrays are fundamental and widely used data structures. They find frequent application in various algorithms and serve in the implementation of complex data structures.

  • Random access: Arrays are ideal for storing data when random sampling is required. By generating a random sequence based on indices, we can achieve random sampling efficiently.
  • Sorting and searching: Arrays are the most commonly used data structure for sorting and searching algorithms. Techniques like quick sort, merge sort, binary search, etc., are primarily operate on arrays.
  • Lookup tables: Arrays serve as efficient lookup tables for quick element or relationship retrieval. For instance, mapping characters to ASCII codes becomes seamless by using the ASCII code values as indices and storing corresponding elements in the array.
  • Machine learning: Within the domain of neural networks, arrays play a pivotal role in executing crucial linear algebra operations involving vectors, matrices, and tensors. Arrays serve as the primary and most extensively used data structure in neural network programming.
  • Data structure implementation: Arrays serve as the building blocks for implementing various data structures like stacks, queues, hash tables, heaps, graphs, etc. For instance, the adjacency matrix representation of a graph is essentially a two-dimensional array.
Feel free to drop your insights, questions or suggestions