The "array" is a linear data structure that stores elements of the same type in contiguous memory locations. We refer to the position of an element in the array as its "index". The following image illustrates the main terminology and concepts of an array.
There are two ways to initialize arrays depending on the requirements: without initial values and with given initial values. In cases where initial values are not specified, most programming languages will initialize the array elements to \(0\):
/* Initialize array */vararr[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}
Elements in an array are stored in contiguous memory locations, which makes it easy to compute the memory address of any element. Given the memory address of the array (the address of the first element) and the index of an element, we can calculate the memory address of that element using the formula shown in the following image, allowing direct access to the element.
Figure 4-2 Memory Address Calculation for Array Elements
As observed in the above image, the index of the first element of an array is \(0\), which may seem counterintuitive since counting starts from \(1\). However, from the perspective of the address calculation formula, an index is essentially an offset from the memory address. The offset for the first element's address is \(0\), making its index \(0\) logical.
Accessing elements in an array is highly efficient, allowing us to randomly access any element in \(O(1)\) time.
As shown in the image below, to insert an element in the middle of an array, all elements following the insertion point must be moved one position back to make room for the new element.
Figure 4-3 Array Element Insertion Example
It's important to note that since the length of an array is fixed, inserting an element will inevitably lead to the loss of the last element in the array. We will discuss solutions to this problem in the "List" chapter.
array.py
definsert(nums:list[int],num:int,index:int):"""在数组的索引 index 处插入元素 num"""# 把索引 index 以及之后的所有元素向后移动一位foriinrange(len(nums)-1,index,-1):nums[i]=nums[i-1]# 将 num 赋给 index 处的元素nums[index]=num
array.cpp
/* 在数组的索引 index 处插入元素 num */voidinsert(int*nums,intsize,intnum,intindex){// 把索引 index 以及之后的所有元素向后移动一位for(inti=size-1;i>index;i--){nums[i]=nums[i-1];}// 将 num 赋给 index 处的元素nums[index]=num;}
array.java
/* 在数组的索引 index 处插入元素 num */voidinsert(int[]nums,intnum,intindex){// 把索引 index 以及之后的所有元素向后移动一位for(inti=nums.length-1;i>index;i--){nums[i]=nums[i-1];}// 将 num 赋给 index 处的元素nums[index]=num;}
array.cs
/* 在数组的索引 index 处插入元素 num */voidInsert(int[]nums,intnum,intindex){// 把索引 index 以及之后的所有元素向后移动一位for(inti=nums.Length-1;i>index;i--){nums[i]=nums[i-1];}// 将 num 赋给 index 处的元素nums[index]=num;}
array.go
/* 在数组的索引 index 处插入元素 num */funcinsert(nums[]int,numint,indexint){// 把索引 index 以及之后的所有元素向后移动一位fori:=len(nums)-1;i>index;i--{nums[i]=nums[i-1]}// 将 num 赋给 index 处的元素nums[index]=num}
array.swift
/* 在数组的索引 index 处插入元素 num */funcinsert(nums:inout[Int],num:Int,index:Int){// 把索引 index 以及之后的所有元素向后移动一位foriinnums.indices.dropFirst(index).reversed(){nums[i]=nums[i-1]}// 将 num 赋给 index 处的元素nums[index]=num}
array.js
/* 在数组的索引 index 处插入元素 num */functioninsert(nums,num,index){// 把索引 index 以及之后的所有元素向后移动一位for(leti=nums.length-1;i>index;i--){nums[i]=nums[i-1];}// 将 num 赋给 index 处的元素nums[index]=num;}
array.ts
/* 在数组的索引 index 处插入元素 num */functioninsert(nums:number[],num:number,index:number):void{// 把索引 index 以及之后的所有元素向后移动一位for(leti=nums.length-1;i>index;i--){nums[i]=nums[i-1];}// 将 num 赋给 index 处的元素nums[index]=num;}
array.dart
/* 在数组的索引 index 处插入元素 _num */voidinsert(List<int>nums,int_num,intindex){// 把索引 index 以及之后的所有元素向后移动一位for(vari=nums.length-1;i>index;i--){nums[i]=nums[i-1];}// 将 _num 赋给 index 处元素nums[index]=_num;}
array.rs
/* 在数组的索引 index 处插入元素 num */fninsert(nums: &mutVec<i32>,num: i32,index: usize){// 把索引 index 以及之后的所有元素向后移动一位foriin(index+1..nums.len()).rev(){nums[i]=nums[i-1];}// 将 num 赋给 index 处的元素nums[index]=num;}
array.c
/* 在数组的索引 index 处插入元素 num */voidinsert(int*nums,intsize,intnum,intindex){// 把索引 index 以及之后的所有元素向后移动一位for(inti=size-1;i>index;i--){nums[i]=nums[i-1];}// 将 num 赋给 index 处的元素nums[index]=num;}
array.zig
// 在数组的索引 index 处插入元素 numfninsert(nums:[]i32,num:i32,index:usize)void{// 把索引 index 以及之后的所有元素向后移动一位vari=nums.len-1;while(i>index):(i-=1){nums[i]=nums[i-1];}// 将 num 赋给 index 处的元素nums[index]=num;}
Similarly, as illustrated below, to delete an element at index \(i\), all elements following index \(i\) must be moved forward by one position.
Figure 4-4 Array Element Deletion Example
Note that after deletion, the last element becomes "meaningless", so we do not need to specifically modify it.
array.py
defremove(nums:list[int],index:int):"""删除索引 index 处的元素"""# 把索引 index 之后的所有元素向前移动一位foriinrange(index,len(nums)-1):nums[i]=nums[i+1]
array.cpp
/* 删除索引 index 处的元素 */voidremove(int*nums,intsize,intindex){// 把索引 index 之后的所有元素向前移动一位for(inti=index;i<size-1;i++){nums[i]=nums[i+1];}}
array.java
/* 删除索引 index 处的元素 */voidremove(int[]nums,intindex){// 把索引 index 之后的所有元素向前移动一位for(inti=index;i<nums.length-1;i++){nums[i]=nums[i+1];}}
array.cs
/* 删除索引 index 处的元素 */voidRemove(int[]nums,intindex){// 把索引 index 之后的所有元素向前移动一位for(inti=index;i<nums.Length-1;i++){nums[i]=nums[i+1];}}
array.go
/* 删除索引 index 处的元素 */funcremove(nums[]int,indexint){// 把索引 index 之后的所有元素向前移动一位fori:=index;i<len(nums)-1;i++{nums[i]=nums[i+1]}}
array.swift
/* 删除索引 index 处的元素 */funcremove(nums:inout[Int],index:Int){// 把索引 index 之后的所有元素向前移动一位foriinnums.indices.dropFirst(index).dropLast(){nums[i]=nums[i+1]}}
array.js
/* 删除索引 index 处的元素 */functionremove(nums,index){// 把索引 index 之后的所有元素向前移动一位for(leti=index;i<nums.length-1;i++){nums[i]=nums[i+1];}}
array.ts
/* 删除索引 index 处的元素 */functionremove(nums:number[],index:number):void{// 把索引 index 之后的所有元素向前移动一位for(leti=index;i<nums.length-1;i++){nums[i]=nums[i+1];}}
array.dart
/* 删除索引 index 处的元素 */voidremove(List<int>nums,intindex){// 把索引 index 之后的所有元素向前移动一位for(vari=index;i<nums.length-1;i++){nums[i]=nums[i+1];}}
array.rs
/* 删除索引 index 处的元素 */fnremove(nums: &mutVec<i32>,index: usize){// 把索引 index 之后的所有元素向前移动一位foriinindex..nums.len()-1{nums[i]=nums[i+1];}}
array.c
/* 删除索引 index 处的元素 */// 注意:stdio.h 占用了 remove 关键词voidremoveItem(int*nums,intsize,intindex){// 把索引 index 之后的所有元素向前移动一位for(inti=index;i<size-1;i++){nums[i]=nums[i+1];}}
array.zig
// 删除索引 index 处的元素fnremove(nums:[]i32,index:usize)void{// 把索引 index 之后的所有元素向前移动一位vari=index;while(i<nums.len-1):(i+=1){nums[i]=nums[i+1];}}
Overall, the insertion and deletion operations in arrays have 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: We can initialize a longer array and use only the front part, allowing the "lost" end elements during insertion to be "meaningless", but this leads to some wasted memory space.
In complex system environments, it's challenging to ensure that the memory space following an array is available, making it unsafe to extend the array's capacity. Therefore, in most programming languages, the length of an array is immutable.
To expand an array, we need 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 is as follows:
Arrays are stored in contiguous memory spaces and consist of elements of the same type. This approach includes a wealth of prior information that the system can use to optimize the operation efficiency of the data structure.
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, leveraging high-speed cache to improve the speed of subsequent operations.
However, continuous space storage is a double-edged sword, with the following limitations:
Low Efficiency in Insertion and Deletion: When there are many elements in an array, insertion and deletion operations require moving 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, which is costly.
Space Wastage: If the allocated size of an array exceeds the actual need, the extra space is wasted.
Arrays are a fundamental and common data structure, frequently used in various algorithms and in implementing complex data structures.
Random Access: If we want to randomly sample some data, we can use an array for storage and generate a random sequence to implement random sampling based on indices.
Sorting and Searching: Arrays are the most commonly used data structure for sorting and searching algorithms. Quick sort, merge sort, binary search, etc., are primarily conducted on arrays.
Lookup Tables: Arrays can be used as lookup tables for fast element or relationship retrieval. For instance, if we want to implement a mapping from characters to ASCII codes, we can use the ASCII code value of a character as the index, with the corresponding element stored in the corresponding position in the array.
Machine Learning: Arrays are extensively used in neural networks for linear algebra operations between vectors, matrices, and tensors. Arrays are the most commonly used data structure in neural network programming.
Data Structure Implementation: Arrays can be used to implement stacks, queues, hash tables, heaps, graphs, etc. For example, the adjacency matrix representation of a graph is essentially a two-dimensional array.
Feel free to drop your insights, questions or suggestions