2023-02-07 21:24:27 +08:00
|
|
|
/**
|
|
|
|
* File: array.dart
|
|
|
|
* Created Time: 2023-01-20
|
|
|
|
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
|
|
|
*/
|
|
|
|
|
2023-10-25 23:15:53 +08:00
|
|
|
// ignore_for_file: unused_local_variable
|
|
|
|
|
2023-02-07 21:24:27 +08:00
|
|
|
import 'dart:math';
|
|
|
|
|
2023-08-17 05:13:19 +08:00
|
|
|
/* 随机访问元素 */
|
2023-11-14 21:27:35 +08:00
|
|
|
int randomAccess(List<int> nums) {
|
2023-06-02 14:56:29 +08:00
|
|
|
// 在区间 [0, nums.length) 中随机抽取一个数字
|
2023-06-02 02:40:26 +08:00
|
|
|
int randomIndex = Random().nextInt(nums.length);
|
|
|
|
// 获取并返回随机元素
|
|
|
|
int randomNum = nums[randomIndex];
|
|
|
|
return randomNum;
|
|
|
|
}
|
2023-02-07 21:24:27 +08:00
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
/* 扩展数组长度 */
|
2023-11-14 21:27:35 +08:00
|
|
|
List<int> extend(List<int> nums, int enlarge) {
|
2023-06-02 14:56:29 +08:00
|
|
|
// 初始化一个扩展长度后的数组
|
2023-06-02 02:40:26 +08:00
|
|
|
List<int> res = List.filled(nums.length + enlarge, 0);
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
for (var i = 0; i < nums.length; i++) {
|
|
|
|
res[i] = nums[i];
|
2023-02-07 21:24:27 +08:00
|
|
|
}
|
2023-06-02 02:40:26 +08:00
|
|
|
// 返回扩展后的新数组
|
|
|
|
return res;
|
|
|
|
}
|
2023-02-07 21:24:27 +08:00
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
2023-11-14 21:27:35 +08:00
|
|
|
void insert(List<int> nums, int num, int index) {
|
2023-06-02 14:56:29 +08:00
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
2023-06-02 02:40:26 +08:00
|
|
|
for (var i = nums.length - 1; i > index; i--) {
|
|
|
|
nums[i] = nums[i - 1];
|
2023-02-07 21:24:27 +08:00
|
|
|
}
|
2023-06-02 02:40:26 +08:00
|
|
|
// 将 num 赋给 index 处元素
|
|
|
|
nums[index] = num;
|
|
|
|
}
|
2023-02-07 21:24:27 +08:00
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
/* 删除索引 index 处元素 */
|
2023-11-14 21:27:35 +08:00
|
|
|
void remove(List<int> nums, int index) {
|
2023-06-02 14:56:29 +08:00
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
2023-06-02 02:40:26 +08:00
|
|
|
for (var i = index; i < nums.length - 1; i++) {
|
|
|
|
nums[i] = nums[i + 1];
|
2023-02-07 21:24:27 +08:00
|
|
|
}
|
2023-06-02 02:40:26 +08:00
|
|
|
}
|
2023-02-07 21:24:27 +08:00
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
/* 遍历数组元素 */
|
2023-11-14 21:27:35 +08:00
|
|
|
void traverse(List<int> nums) {
|
|
|
|
int count = 0;
|
2023-06-02 02:40:26 +08:00
|
|
|
// 通过索引遍历数组
|
|
|
|
for (var i = 0; i < nums.length; i++) {
|
2023-11-14 21:27:35 +08:00
|
|
|
count += nums[i];
|
2023-02-07 21:24:27 +08:00
|
|
|
}
|
2023-11-14 21:27:35 +08:00
|
|
|
// 直接遍历数组元素
|
|
|
|
for (int num in nums) {
|
|
|
|
count += num;
|
2023-06-02 02:40:26 +08:00
|
|
|
}
|
2023-06-02 14:56:29 +08:00
|
|
|
// 通过 forEach 方法遍历数组
|
2023-11-14 21:27:35 +08:00
|
|
|
nums.forEach((num) {
|
|
|
|
count += num;
|
2023-06-02 02:40:26 +08:00
|
|
|
});
|
|
|
|
}
|
2023-02-07 21:24:27 +08:00
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
/* 在数组中查找指定元素 */
|
2023-11-14 21:27:35 +08:00
|
|
|
int find(List<int> nums, int target) {
|
2023-06-02 02:40:26 +08:00
|
|
|
for (var i = 0; i < nums.length; i++) {
|
|
|
|
if (nums[i] == target) return i;
|
2023-02-07 21:24:27 +08:00
|
|
|
}
|
2023-06-02 02:40:26 +08:00
|
|
|
return -1;
|
2023-02-07 21:24:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
2023-06-02 14:56:29 +08:00
|
|
|
void main() {
|
|
|
|
/* 初始化数组 */
|
2023-02-07 21:24:27 +08:00
|
|
|
var arr = List.filled(5, 0);
|
|
|
|
print('数组 arr = $arr');
|
2023-11-14 21:27:35 +08:00
|
|
|
List<int> nums = [1, 3, 2, 5, 4];
|
2023-02-07 21:24:27 +08:00
|
|
|
print('数组 nums = $nums');
|
|
|
|
|
|
|
|
/* 随机访问 */
|
2023-06-02 02:40:26 +08:00
|
|
|
int randomNum = randomAccess(nums);
|
2023-02-07 21:24:27 +08:00
|
|
|
print('在 nums 中获取随机元素 $randomNum');
|
|
|
|
|
|
|
|
/* 长度扩展 */
|
2023-06-02 02:40:26 +08:00
|
|
|
nums = extend(nums, 3);
|
2023-05-30 13:00:16 +08:00
|
|
|
print('将数组长度扩展至 8 ,得到 nums = $nums');
|
2023-02-07 21:24:27 +08:00
|
|
|
|
|
|
|
/* 插入元素 */
|
2023-06-02 02:40:26 +08:00
|
|
|
insert(nums, 6, 3);
|
2023-02-07 21:24:27 +08:00
|
|
|
print("在索引 3 处插入数字 6 ,得到 nums = $nums");
|
|
|
|
|
|
|
|
/* 删除元素 */
|
2023-06-02 02:40:26 +08:00
|
|
|
remove(nums, 2);
|
2023-02-07 21:24:27 +08:00
|
|
|
print("删除索引 2 处的元素,得到 nums = $nums");
|
|
|
|
|
2023-11-14 21:27:35 +08:00
|
|
|
/* 遍历数组 */
|
2023-06-02 02:40:26 +08:00
|
|
|
traverse(nums);
|
2023-02-07 21:24:27 +08:00
|
|
|
|
|
|
|
/* 查找元素 */
|
2023-06-02 02:40:26 +08:00
|
|
|
int index = find(nums, 3);
|
2023-02-07 21:24:27 +08:00
|
|
|
print("在 nums 中查找元素 3 ,得到索引 = $index");
|
|
|
|
}
|