2023-02-28 20:03:53 +08:00
|
|
|
|
# 列表
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
「列表 list」是一个抽象的数据结构概念,它表示元素的有序集合,支持元素访问、修改、添加、删除和遍历等操作,无须使用者考虑容量限制的问题。列表可以基于链表或数组实现。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-12-14 02:42:53 +08:00
|
|
|
|
- 链表天然可以看作一个列表,其支持元素增删查改操作,并且可以灵活动态扩容。
|
|
|
|
|
- 数组也支持元素增删查改,但由于其长度不可变,因此只能看作一个具有长度限制的列表。
|
2023-10-17 01:06:00 +08:00
|
|
|
|
|
2023-12-14 02:42:53 +08:00
|
|
|
|
当使用数组实现列表时,**长度不可变的性质会导致列表的实用性降低**。这是因为我们通常无法事先确定需要存储多少数据,从而难以选择合适的列表长度。若长度过小,则很可能无法满足使用需求;若长度过大,则会造成内存空间浪费。
|
2023-10-17 01:06:00 +08:00
|
|
|
|
|
|
|
|
|
为解决此问题,我们可以使用「动态数组 dynamic array」来实现列表。它继承了数组的各项优点,并且可以在程序运行过程中进行动态扩容。
|
|
|
|
|
|
2023-12-14 02:42:53 +08:00
|
|
|
|
实际上,**许多编程语言中的标准库提供的列表是基于动态数组实现的**,例如 Python 中的 `list` 、Java 中的 `ArrayList` 、C++ 中的 `vector` 和 C# 中的 `List` 等。在接下来的讨论中,我们将把“列表”和“动态数组”视为等同的概念。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 列表常用操作
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-08-17 05:13:19 +08:00
|
|
|
|
### 初始化列表
|
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
我们通常使用“无初始值”和“有初始值”这两种初始化方法:
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Python"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```python title="list.py"
|
|
|
|
|
# 初始化列表
|
|
|
|
|
# 无初始值
|
2023-10-27 23:24:13 +08:00
|
|
|
|
nums1: list[int] = []
|
2023-09-04 03:19:08 +08:00
|
|
|
|
# 有初始值
|
2023-10-27 23:24:13 +08:00
|
|
|
|
nums: list[int] = [1, 3, 2, 5, 4]
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="list.cpp"
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 初始化列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
// 需注意,C++ 中 vector 即是本文描述的 nums
|
2022-12-21 01:37:58 +08:00
|
|
|
|
// 无初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
vector<int> nums1;
|
2022-12-21 01:37:58 +08:00
|
|
|
|
// 有初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
vector<int> nums = { 1, 3, 2, 5, 4 };
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Java"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```java title="list.java"
|
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
List<Integer> nums1 = new ArrayList<>();
|
2023-09-04 03:19:08 +08:00
|
|
|
|
// 有初始值(注意数组的元素类型需为 int[] 的包装类 Integer[])
|
|
|
|
|
Integer[] numbers = new Integer[] { 1, 3, 2, 5, 4 };
|
2023-10-09 18:20:42 +08:00
|
|
|
|
List<Integer> nums = new ArrayList<>(Arrays.asList(numbers));
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="list.cs"
|
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-11-26 23:18:44 +08:00
|
|
|
|
List<int> nums1 = [];
|
2023-09-04 03:19:08 +08:00
|
|
|
|
// 有初始值
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int[] numbers = [1, 3, 2, 5, 4];
|
|
|
|
|
List<int> nums = [.. numbers];
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
2022-12-18 14:55:14 +08:00
|
|
|
|
```go title="list_test.go"
|
|
|
|
|
/* 初始化列表 */
|
2022-12-21 01:37:58 +08:00
|
|
|
|
// 无初始值
|
2023-11-20 02:08:29 +08:00
|
|
|
|
nums1 := []int{}
|
2022-12-21 01:37:58 +08:00
|
|
|
|
// 有初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums := []int{1, 3, 2, 5, 4}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="list.swift"
|
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
let nums1: [Int] = []
|
2023-09-04 03:19:08 +08:00
|
|
|
|
// 有初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
var nums = [1, 3, 2, 5, 4]
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="list.js"
|
2022-12-12 21:30:55 +08:00
|
|
|
|
/* 初始化列表 */
|
2022-12-21 01:37:58 +08:00
|
|
|
|
// 无初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
const nums1 = [];
|
2022-12-21 01:37:58 +08:00
|
|
|
|
// 有初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
const nums = [1, 3, 2, 5, 4];
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="list.ts"
|
2022-12-12 03:22:40 +08:00
|
|
|
|
/* 初始化列表 */
|
2022-12-21 01:37:58 +08:00
|
|
|
|
// 无初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
const nums1: number[] = [];
|
2022-12-21 01:37:58 +08:00
|
|
|
|
// 有初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
const nums: number[] = [1, 3, 2, 5, 4];
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Dart"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```dart title="list.dart"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
List<int> nums1 = [];
|
2023-02-09 19:45:35 +08:00
|
|
|
|
// 有初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
List<int> nums = [1, 3, 2, 5, 4];
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Rust"
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```rust title="list.rs"
|
2023-01-08 23:44:13 +08:00
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
// 无初始值
|
2023-10-09 18:20:42 +08:00
|
|
|
|
let nums1: Vec<i32> = Vec::new();
|
2023-01-08 23:44:13 +08:00
|
|
|
|
// 有初始值
|
2023-10-10 13:16:09 +08:00
|
|
|
|
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="list.c"
|
|
|
|
|
// C 未提供内置动态数组
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 03:15:36 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="list.kt"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="list.zig"
|
2023-02-03 19:15:34 +08:00
|
|
|
|
// 初始化列表
|
2023-10-09 18:20:42 +08:00
|
|
|
|
var nums = std.ArrayList(i32).init(std.heap.page_allocator);
|
|
|
|
|
defer nums.deinit();
|
|
|
|
|
try nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 04:04:01 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:09:35 +08:00
|
|
|
|
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20%23%20%E6%97%A0%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums1%20%3D%20%5B%5D%0A%20%20%20%20%23%20%E6%9C%89%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D&cumulative=false&curInstr=4&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
2024-01-07 04:04:01 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
### 访问元素
|
2023-06-02 02:40:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
列表本质上是数组,因此可以在 $O(1)$ 时间内访问和更新元素,效率很高。
|
2023-06-02 02:40:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Python"
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```python title="list.py"
|
|
|
|
|
# 访问元素
|
2023-10-10 13:16:09 +08:00
|
|
|
|
num: int = nums[1] # 访问索引 1 处的元素
|
2023-09-04 03:19:08 +08:00
|
|
|
|
|
|
|
|
|
# 更新元素
|
2023-10-10 13:16:09 +08:00
|
|
|
|
nums[1] = 0 # 将索引 1 处的元素更新为 0
|
2023-07-26 11:00:53 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C++"
|
2023-08-17 05:13:19 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```cpp title="list.cpp"
|
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
int num = nums[1]; // 访问索引 1 处的元素
|
2023-09-04 03:19:08 +08:00
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="list.java"
|
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
int num = nums.get(1); // 访问索引 1 处的元素
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.set(1, 0); // 将索引 1 处的元素更新为 0
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C#"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```csharp title="list.cs"
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
int num = nums[1]; // 访问索引 1 处的元素
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
2022-12-18 14:55:14 +08:00
|
|
|
|
```go title="list_test.go"
|
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
num := nums[1] // 访问索引 1 处的元素
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-18 14:55:14 +08:00
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums[1] = 0 // 将索引 1 处的元素更新为 0
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="list.swift"
|
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
let num = nums[1] // 访问索引 1 处的元素
|
2023-09-04 03:19:08 +08:00
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums[1] = 0 // 将索引 1 处的元素更新为 0
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="list.js"
|
2022-12-12 21:30:55 +08:00
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
const num = nums[1]; // 访问索引 1 处的元素
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-12 21:30:55 +08:00
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="list.ts"
|
2022-12-12 03:22:40 +08:00
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
const num: number = nums[1]; // 访问索引 1 处的元素
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-12 03:22:40 +08:00
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Dart"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```dart title="list.dart"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
int num = nums[1]; // 访问索引 1 处的元素
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Rust"
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```rust title="list.rs"
|
2023-01-08 23:44:13 +08:00
|
|
|
|
/* 访问元素 */
|
2023-10-17 23:45:31 +08:00
|
|
|
|
let num: i32 = nums[1]; // 访问索引 1 处的元素
|
2023-01-08 23:44:13 +08:00
|
|
|
|
/* 更新元素 */
|
2023-10-17 23:45:31 +08:00
|
|
|
|
nums[1] = 0; // 将索引 1 处的元素更新为 0
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="list.c"
|
|
|
|
|
// C 未提供内置动态数组
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 03:15:36 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="list.kt"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="list.zig"
|
2023-02-03 19:15:34 +08:00
|
|
|
|
// 访问元素
|
2023-10-09 18:20:42 +08:00
|
|
|
|
var num = nums.items[1]; // 访问索引 1 处的元素
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
2023-02-03 19:15:34 +08:00
|
|
|
|
// 更新元素
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.items[1] = 0; // 将索引 1 处的元素更新为 0
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 04:04:01 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:09:35 +08:00
|
|
|
|
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%0A%20%20%20%20num%20%3D%20nums%5B1%5D%20%20%23%20%E8%AE%BF%E9%97%AE%E7%B4%A2%E5%BC%95%201%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%0A%0A%20%20%20%20%23%20%E6%9B%B4%E6%96%B0%E5%85%83%E7%B4%A0%0A%20%20%20%20nums%5B1%5D%20%3D%200%20%20%20%20%23%20%E5%B0%86%E7%B4%A2%E5%BC%95%201%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%E6%9B%B4%E6%96%B0%E4%B8%BA%200&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
2024-01-07 04:04:01 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
### 插入与删除元素
|
2023-06-02 02:40:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
相较于数组,列表可以自由地添加与删除元素。在列表尾部添加元素的时间复杂度为 $O(1)$ ,但插入和删除元素的效率仍与数组相同,时间复杂度为 $O(n)$ 。
|
2023-06-02 02:40:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Python"
|
2023-06-02 02:40:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```python title="list.py"
|
|
|
|
|
# 清空列表
|
2023-10-10 13:16:09 +08:00
|
|
|
|
nums.clear()
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
# 在尾部添加元素
|
2023-10-10 13:16:09 +08:00
|
|
|
|
nums.append(1)
|
|
|
|
|
nums.append(3)
|
|
|
|
|
nums.append(2)
|
|
|
|
|
nums.append(5)
|
|
|
|
|
nums.append(4)
|
2023-09-04 03:19:08 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
# 在中间插入元素
|
2023-10-10 13:16:09 +08:00
|
|
|
|
nums.insert(3, 6) # 在索引 3 处插入数字 6
|
2023-09-04 03:19:08 +08:00
|
|
|
|
|
|
|
|
|
# 删除元素
|
2023-10-10 13:16:09 +08:00
|
|
|
|
nums.pop(3) # 删除索引 3 处的元素
|
2023-07-26 11:00:53 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C++"
|
2023-08-17 05:13:19 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```cpp title="list.cpp"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.clear();
|
2023-09-04 03:19:08 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.push_back(1);
|
|
|
|
|
nums.push_back(3);
|
|
|
|
|
nums.push_back(2);
|
|
|
|
|
nums.push_back(5);
|
|
|
|
|
nums.push_back(4);
|
2023-09-04 03:19:08 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.insert(nums.begin() + 3, 6); // 在索引 3 处插入数字 6
|
2023-09-04 03:19:08 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.erase(nums.begin() + 3); // 删除索引 3 处的元素
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="list.java"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.clear();
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.add(1);
|
|
|
|
|
nums.add(3);
|
|
|
|
|
nums.add(2);
|
|
|
|
|
nums.add(5);
|
|
|
|
|
nums.add(4);
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.add(3, 6); // 在索引 3 处插入数字 6
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.remove(3); // 删除索引 3 处的元素
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C#"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```csharp title="list.cs"
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.Clear();
|
2022-11-27 19:07:35 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.Add(1);
|
|
|
|
|
nums.Add(3);
|
|
|
|
|
nums.Add(2);
|
|
|
|
|
nums.Add(5);
|
|
|
|
|
nums.Add(4);
|
2022-11-27 19:07:35 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.Insert(3, 6);
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.RemoveAt(3);
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
2022-12-18 14:55:14 +08:00
|
|
|
|
```go title="list_test.go"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums = nil
|
2022-12-18 14:55:14 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums = append(nums, 1)
|
|
|
|
|
nums = append(nums, 3)
|
|
|
|
|
nums = append(nums, 2)
|
|
|
|
|
nums = append(nums, 5)
|
|
|
|
|
nums = append(nums, 4)
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums = append(nums[:3], append([]int{6}, nums[3:]...)...) // 在索引 3 处插入数字 6
|
2022-12-18 14:55:14 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums = append(nums[:3], nums[4:]...) // 删除索引 3 处的元素
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="list.swift"
|
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.removeAll()
|
2023-09-04 03:19:08 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.append(1)
|
|
|
|
|
nums.append(3)
|
|
|
|
|
nums.append(2)
|
|
|
|
|
nums.append(5)
|
|
|
|
|
nums.append(4)
|
2023-09-04 03:19:08 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.insert(6, at: 3) // 在索引 3 处插入数字 6
|
2023-09-04 03:19:08 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.remove(at: 3) // 删除索引 3 处的元素
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="list.js"
|
2022-12-12 21:30:55 +08:00
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.length = 0;
|
2022-12-12 21:30:55 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.push(1);
|
|
|
|
|
nums.push(3);
|
|
|
|
|
nums.push(2);
|
|
|
|
|
nums.push(5);
|
|
|
|
|
nums.push(4);
|
2022-12-12 21:30:55 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.splice(3, 0, 6);
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-12 21:30:55 +08:00
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.splice(3, 1);
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="list.ts"
|
2022-12-12 03:22:40 +08:00
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.length = 0;
|
2022-12-12 03:22:40 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.push(1);
|
|
|
|
|
nums.push(3);
|
|
|
|
|
nums.push(2);
|
|
|
|
|
nums.push(5);
|
|
|
|
|
nums.push(4);
|
2022-12-12 03:22:40 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.splice(3, 0, 6);
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-12 03:22:40 +08:00
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.splice(3, 1);
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Dart"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```dart title="list.dart"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.clear();
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.add(1);
|
|
|
|
|
nums.add(3);
|
|
|
|
|
nums.add(2);
|
|
|
|
|
nums.add(5);
|
|
|
|
|
nums.add(4);
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.insert(3, 6); // 在索引 3 处插入数字 6
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.removeAt(3); // 删除索引 3 处的元素
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Rust"
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```rust title="list.rs"
|
2023-01-08 23:44:13 +08:00
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.clear();
|
2023-01-08 23:44:13 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在尾部添加元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.push(1);
|
|
|
|
|
nums.push(3);
|
|
|
|
|
nums.push(2);
|
|
|
|
|
nums.push(5);
|
|
|
|
|
nums.push(4);
|
2023-01-08 23:44:13 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 在中间插入元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.insert(3, 6); // 在索引 3 处插入数字 6
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-01-08 23:44:13 +08:00
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.remove(3); // 删除索引 3 处的元素
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="list.c"
|
|
|
|
|
// C 未提供内置动态数组
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 03:15:36 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="list.kt"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="list.zig"
|
2023-02-03 19:15:34 +08:00
|
|
|
|
// 清空列表
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.clearRetainingCapacity();
|
2023-02-03 19:15:34 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
// 在尾部添加元素
|
2023-10-09 18:20:42 +08:00
|
|
|
|
try nums.append(1);
|
|
|
|
|
try nums.append(3);
|
|
|
|
|
try nums.append(2);
|
|
|
|
|
try nums.append(5);
|
|
|
|
|
try nums.append(4);
|
2023-02-03 19:15:34 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
// 在中间插入元素
|
2023-10-09 18:20:42 +08:00
|
|
|
|
try nums.insert(3, 6); // 在索引 3 处插入数字 6
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
2023-02-03 19:15:34 +08:00
|
|
|
|
// 删除元素
|
2023-10-09 18:20:42 +08:00
|
|
|
|
_ = nums.orderedRemove(3); // 删除索引 3 处的元素
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 04:04:01 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:09:35 +08:00
|
|
|
|
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E6%9C%89%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%B8%85%E7%A9%BA%E5%88%97%E8%A1%A8%0A%20%20%20%20nums.clear%28%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%9C%A8%E5%B0%BE%E9%83%A8%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.append%281%29%0A%20%20%20%20nums.append%283%29%0A%20%20%20%20nums.append%282%29%0A%20%20%20%20nums.append%285%29%0A%20%20%20%20nums.append%284%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%9C%A8%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.insert%283,%206%29%20%20%23%20%E5%9C%A8%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E6%8F%92%E5%85%A5%E6%95%B0%E5%AD%97%206%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.pop%283%29%20%20%20%20%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
2024-01-07 04:04:01 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
### 遍历列表
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
与数组一样,列表可以根据索引遍历,也可以直接遍历各元素。
|
2023-08-07 18:22:04 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Python"
|
2023-08-07 18:22:04 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```python title="list.py"
|
|
|
|
|
# 通过索引遍历列表
|
|
|
|
|
count = 0
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for i in range(len(nums)):
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += nums[i]
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
# 直接遍历列表元素
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for num in nums:
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += num
|
2023-07-26 11:00:53 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C++"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```cpp title="list.cpp"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
int count = 0;
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for (int i = 0; i < nums.size(); i++) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += nums[i];
|
2022-11-22 17:47:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0;
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for (int num : nums) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += num;
|
2022-11-22 17:47:26 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Java"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```java title="list.java"
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
int count = 0;
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for (int i = 0; i < nums.size(); i++) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += nums.get(i);
|
2022-11-27 19:07:35 +08:00
|
|
|
|
}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 直接遍历列表元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for (int num : nums) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += num;
|
2022-11-27 19:07:35 +08:00
|
|
|
|
}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C#"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```csharp title="list.cs"
|
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
int count = 0;
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for (int i = 0; i < nums.Count; i++) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += nums[i];
|
2023-09-04 03:19:08 +08:00
|
|
|
|
}
|
2022-11-25 03:59:38 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0;
|
2023-10-09 18:20:42 +08:00
|
|
|
|
foreach (int num in nums) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += num;
|
2023-09-04 03:19:08 +08:00
|
|
|
|
}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
2022-12-18 14:55:14 +08:00
|
|
|
|
```go title="list_test.go"
|
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
count := 0
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for i := 0; i < len(nums); i++ {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += nums[i]
|
2022-12-18 14:55:14 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-18 14:55:14 +08:00
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0
|
2023-11-14 21:27:35 +08:00
|
|
|
|
for _, num := range nums {
|
|
|
|
|
count += num
|
2022-12-24 16:15:41 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="list.swift"
|
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
var count = 0
|
2023-11-14 21:27:35 +08:00
|
|
|
|
for i in nums.indices {
|
|
|
|
|
count += nums[i]
|
2023-09-04 03:19:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0
|
2023-11-14 21:27:35 +08:00
|
|
|
|
for num in nums {
|
|
|
|
|
count += num
|
2023-09-04 03:19:08 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="list.js"
|
2022-12-12 21:30:55 +08:00
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
let count = 0;
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += nums[i];
|
2022-12-12 21:30:55 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-12 21:30:55 +08:00
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0;
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for (const num of nums) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += num;
|
2022-12-12 21:30:55 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="list.ts"
|
2022-12-12 03:22:40 +08:00
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
let count = 0;
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += nums[i];
|
2022-12-12 03:22:40 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2022-12-12 03:22:40 +08:00
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0;
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for (const num of nums) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += num;
|
2022-12-12 03:22:40 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Dart"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```dart title="list.dart"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
int count = 0;
|
2023-11-14 21:27:35 +08:00
|
|
|
|
for (var i = 0; i < nums.length; i++) {
|
|
|
|
|
count += nums[i];
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-11-14 21:27:35 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0;
|
2023-11-14 21:27:35 +08:00
|
|
|
|
for (var num in nums) {
|
|
|
|
|
count += num;
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Rust"
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```rust title="list.rs"
|
2023-11-14 21:27:35 +08:00
|
|
|
|
// 通过索引遍历列表
|
|
|
|
|
let mut _count = 0;
|
|
|
|
|
for i in 0..nums.len() {
|
|
|
|
|
_count += nums[i];
|
2023-01-08 23:44:13 +08:00
|
|
|
|
}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-11-14 21:27:35 +08:00
|
|
|
|
// 直接遍历列表元素
|
|
|
|
|
_count = 0;
|
|
|
|
|
for num in &nums {
|
|
|
|
|
_count += num;
|
2023-01-08 23:44:13 +08:00
|
|
|
|
}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="list.c"
|
|
|
|
|
// C 未提供内置动态数组
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 03:15:36 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="list.kt"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="list.zig"
|
2023-02-03 19:15:34 +08:00
|
|
|
|
// 通过索引遍历列表
|
|
|
|
|
var count: i32 = 0;
|
|
|
|
|
var i: i32 = 0;
|
2023-10-09 18:20:42 +08:00
|
|
|
|
while (i < nums.items.len) : (i += 1) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += nums[i];
|
2023-02-03 19:15:34 +08:00
|
|
|
|
}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
2023-02-03 19:15:34 +08:00
|
|
|
|
// 直接遍历列表元素
|
|
|
|
|
count = 0;
|
2023-11-14 21:27:35 +08:00
|
|
|
|
for (nums.items) |num| {
|
|
|
|
|
count += num;
|
2023-02-03 19:15:34 +08:00
|
|
|
|
}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 04:04:01 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:09:35 +08:00
|
|
|
|
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E9%80%9A%E8%BF%87%E7%B4%A2%E5%BC%95%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20nums%5Bi%5D%0A%0A%20%20%20%20%23%20%E7%9B%B4%E6%8E%A5%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0%0A%20%20%20%20for%20num%20in%20nums%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20num&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
2024-01-07 04:04:01 +08:00
|
|
|
|
|
2023-08-17 05:13:19 +08:00
|
|
|
|
### 拼接列表
|
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
给定一个新列表 `nums1` ,我们可以将其拼接到原列表的尾部。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Python"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```python title="list.py"
|
|
|
|
|
# 拼接两个列表
|
2023-10-27 23:24:13 +08:00
|
|
|
|
nums1: list[int] = [6, 8, 7, 10, 9]
|
2023-10-10 13:16:09 +08:00
|
|
|
|
nums += nums1 # 将列表 nums1 拼接到 nums 之后
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="list.cpp"
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
vector<int> nums1 = { 6, 8, 7, 10, 9 };
|
|
|
|
|
// 将列表 nums1 拼接到 nums 之后
|
|
|
|
|
nums.insert(nums.end(), nums1.begin(), nums1.end());
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Java"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```java title="list.java"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
List<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));
|
|
|
|
|
nums.addAll(nums1); // 将列表 nums1 拼接到 nums 之后
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="list.cs"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
List<int> nums1 = [6, 8, 7, 10, 9];
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.AddRange(nums1); // 将列表 nums1 拼接到 nums 之后
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
2022-12-18 14:55:14 +08:00
|
|
|
|
```go title="list_test.go"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums1 := []int{6, 8, 7, 10, 9}
|
|
|
|
|
nums = append(nums, nums1...) // 将列表 nums1 拼接到 nums 之后
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="list.swift"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
let nums1 = [6, 8, 7, 10, 9]
|
|
|
|
|
nums.append(contentsOf: nums1) // 将列表 nums1 拼接到 nums 之后
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="list.js"
|
2022-12-12 21:30:55 +08:00
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
const nums1 = [6, 8, 7, 10, 9];
|
|
|
|
|
nums.push(...nums1); // 将列表 nums1 拼接到 nums 之后
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="list.ts"
|
2022-12-12 03:22:40 +08:00
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
const nums1: number[] = [6, 8, 7, 10, 9];
|
|
|
|
|
nums.push(...nums1); // 将列表 nums1 拼接到 nums 之后
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Dart"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```dart title="list.dart"
|
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
List<int> nums1 = [6, 8, 7, 10, 9];
|
|
|
|
|
nums.addAll(nums1); // 将列表 nums1 拼接到 nums 之后
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Rust"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```rust title="list.rs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
let nums1: Vec<i32> = vec![6, 8, 7, 10, 9];
|
|
|
|
|
nums.extend(nums1);
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C"
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```c title="list.c"
|
|
|
|
|
// C 未提供内置动态数组
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 03:15:36 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="list.kt"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="list.zig"
|
2023-02-03 19:15:34 +08:00
|
|
|
|
// 拼接两个列表
|
2023-10-09 18:20:42 +08:00
|
|
|
|
var nums1 = std.ArrayList(i32).init(std.heap.page_allocator);
|
|
|
|
|
defer nums1.deinit();
|
|
|
|
|
try nums1.appendSlice(&[_]i32{ 6, 8, 7, 10, 9 });
|
|
|
|
|
try nums.insertSlice(nums.items.len, nums1.items); // 将列表 nums1 拼接到 nums 之后
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 04:04:01 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:09:35 +08:00
|
|
|
|
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%8B%BC%E6%8E%A5%E4%B8%A4%E4%B8%AA%E5%88%97%E8%A1%A8%0A%20%20%20%20nums1%20%3D%20%5B6,%208,%207,%2010,%209%5D%0A%20%20%20%20nums%20%2B%3D%20nums1%20%20%23%20%E5%B0%86%E5%88%97%E8%A1%A8%20nums1%20%E6%8B%BC%E6%8E%A5%E5%88%B0%20nums%20%E4%B9%8B%E5%90%8E&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
2024-01-07 04:04:01 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
### 排序列表
|
2023-06-02 02:40:26 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
完成列表排序后,我们便可以使用在数组类算法题中经常考查的“二分查找”和“双指针”算法。
|
2023-06-02 02:40:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Python"
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```python title="list.py"
|
|
|
|
|
# 排序列表
|
2023-10-10 13:16:09 +08:00
|
|
|
|
nums.sort() # 排序后,列表元素从小到大排列
|
2023-07-26 11:00:53 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C++"
|
2023-08-17 05:13:19 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```cpp title="list.cpp"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
sort(nums.begin(), nums.end()); // 排序后,列表元素从小到大排列
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="list.java"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
Collections.sort(nums); // 排序后,列表元素从小到大排列
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C#"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```csharp title="list.cs"
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.Sort(); // 排序后,列表元素从小到大排列
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
2022-12-18 14:55:14 +08:00
|
|
|
|
```go title="list_test.go"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
sort.Ints(nums) // 排序后,列表元素从小到大排列
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="list.swift"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.sort() // 排序后,列表元素从小到大排列
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="list.js"
|
2022-12-12 21:30:55 +08:00
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.sort((a, b) => a - b); // 排序后,列表元素从小到大排列
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="list.ts"
|
2022-12-12 03:22:40 +08:00
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.sort((a, b) => a - b); // 排序后,列表元素从小到大排列
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Dart"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```dart title="list.dart"
|
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.sort(); // 排序后,列表元素从小到大排列
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Rust"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```rust title="list.rs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.sort(); // 排序后,列表元素从小到大排列
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C"
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```c title="list.c"
|
|
|
|
|
// C 未提供内置动态数组
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 03:15:36 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="list.kt"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="list.zig"
|
2023-02-03 19:15:34 +08:00
|
|
|
|
// 排序列表
|
2023-10-09 18:20:42 +08:00
|
|
|
|
std.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 04:04:01 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:09:35 +08:00
|
|
|
|
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%8E%92%E5%BA%8F%E5%88%97%E8%A1%A8%0A%20%20%20%20nums.sort%28%29%20%20%23%20%E6%8E%92%E5%BA%8F%E5%90%8E%EF%BC%8C%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0%E4%BB%8E%E5%B0%8F%E5%88%B0%E5%A4%A7%E6%8E%92%E5%88%97&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
2024-01-07 04:04:01 +08:00
|
|
|
|
|
2023-08-17 05:13:19 +08:00
|
|
|
|
## 列表实现
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
许多编程语言内置了列表,例如 Java、C++、Python 等。它们的实现比较复杂,各个参数的设定也非常考究,例如初始容量、扩容倍数等。感兴趣的读者可以查阅源码进行学习。
|
2023-08-17 05:13:19 +08:00
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
为了加深对列表工作原理的理解,我们尝试实现一个简易版列表,包括以下三个重点设计。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-07 22:31:50 +08:00
|
|
|
|
- **初始容量**:选取一个合理的数组初始容量。在本示例中,我们选择 10 作为初始容量。
|
2023-09-17 01:13:15 +08:00
|
|
|
|
- **数量记录**:声明一个变量 `size` ,用于记录列表当前元素数量,并随着元素插入和删除实时更新。根据此变量,我们可以定位列表尾部,以及判断是否需要扩容。
|
2023-12-02 06:21:34 +08:00
|
|
|
|
- **扩容机制**:若插入元素时列表容量已满,则需要进行扩容。先根据扩容倍数创建一个更大的数组,再将当前数组的所有元素依次移动至新数组。在本示例中,我们规定每次将数组扩容至之前的 2 倍。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-10-17 01:06:00 +08:00
|
|
|
|
```src
|
|
|
|
|
[file]{my_list}-[class]{my_list}-[func]{}
|
|
|
|
|
```
|