2022-12-24 16:15:41 +08:00
|
|
|
// File: array.cs
|
2022-12-16 15:53:26 +08:00
|
|
|
// Created Time: 2022-12-14
|
|
|
|
// Author: mingXta (1195669834@qq.com)
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
namespace hello_algo.chapter_array_and_linkedlist;
|
|
|
|
|
2023-04-23 03:03:12 +08:00
|
|
|
public class array {
|
2023-08-17 05:13:19 +08:00
|
|
|
/* 随机访问元素 */
|
2023-11-26 23:18:44 +08:00
|
|
|
int RandomAccess(int[] nums) {
|
2023-02-08 22:18:02 +08:00
|
|
|
Random random = new();
|
|
|
|
// 在区间 [0, nums.Length) 中随机抽取一个数字
|
|
|
|
int randomIndex = random.Next(nums.Length);
|
|
|
|
// 获取并返回随机元素
|
|
|
|
int randomNum = nums[randomIndex];
|
|
|
|
return randomNum;
|
|
|
|
}
|
2022-12-14 13:54:49 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 扩展数组长度 */
|
2023-11-26 23:18:44 +08:00
|
|
|
int[] Extend(int[] nums, int enlarge) {
|
2023-02-08 22:18:02 +08:00
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
int[] res = new int[nums.Length + enlarge];
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
2023-04-23 03:03:12 +08:00
|
|
|
for (int i = 0; i < nums.Length; i++) {
|
2023-02-08 22:18:02 +08:00
|
|
|
res[i] = nums[i];
|
2022-12-14 13:54:49 +08:00
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
// 返回扩展后的新数组
|
|
|
|
return res;
|
|
|
|
}
|
2022-12-14 13:54:49 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
2023-11-26 23:18:44 +08:00
|
|
|
void Insert(int[] nums, int num, int index) {
|
2023-02-08 22:18:02 +08:00
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
2023-04-23 03:03:12 +08:00
|
|
|
for (int i = nums.Length - 1; i > index; i--) {
|
2023-02-08 22:18:02 +08:00
|
|
|
nums[i] = nums[i - 1];
|
2022-12-14 13:54:49 +08:00
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
// 将 num 赋给 index 处元素
|
|
|
|
nums[index] = num;
|
|
|
|
}
|
2022-12-14 13:54:49 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 删除索引 index 处元素 */
|
2023-11-26 23:18:44 +08:00
|
|
|
void Remove(int[] nums, int index) {
|
2023-02-08 22:18:02 +08:00
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
2023-04-23 03:03:12 +08:00
|
|
|
for (int i = index; i < nums.Length - 1; i++) {
|
2023-02-08 22:18:02 +08:00
|
|
|
nums[i] = nums[i + 1];
|
2022-12-14 13:54:49 +08:00
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
}
|
2022-12-14 13:54:49 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 遍历数组 */
|
2023-11-26 23:18:44 +08:00
|
|
|
void Traverse(int[] nums) {
|
2023-02-08 22:18:02 +08:00
|
|
|
int count = 0;
|
|
|
|
// 通过索引遍历数组
|
2023-04-23 03:03:12 +08:00
|
|
|
for (int i = 0; i < nums.Length; i++) {
|
2023-11-14 21:27:35 +08:00
|
|
|
count += nums[i];
|
2022-12-14 13:54:49 +08:00
|
|
|
}
|
2023-11-14 21:27:35 +08:00
|
|
|
// 直接遍历数组元素
|
2023-04-23 03:03:12 +08:00
|
|
|
foreach (int num in nums) {
|
2023-11-14 21:27:35 +08:00
|
|
|
count += num;
|
2022-12-14 13:54:49 +08:00
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
}
|
2022-12-14 13:54:49 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 在数组中查找指定元素 */
|
2023-11-26 23:18:44 +08:00
|
|
|
int Find(int[] nums, int target) {
|
2023-04-23 03:03:12 +08:00
|
|
|
for (int i = 0; i < nums.Length; i++) {
|
2023-02-08 22:18:02 +08:00
|
|
|
if (nums[i] == target)
|
|
|
|
return i;
|
2022-12-14 13:54:49 +08:00
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2023-01-10 01:49:16 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 辅助函数,数组转字符串 */
|
2023-11-26 23:18:44 +08:00
|
|
|
string ToString(int[] nums) {
|
2023-02-08 22:18:02 +08:00
|
|
|
return string.Join(",", nums);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
2023-11-26 23:18:44 +08:00
|
|
|
public void Test() {
|
2023-02-08 22:18:02 +08:00
|
|
|
// 初始化数组
|
|
|
|
int[] arr = new int[5];
|
2023-10-08 01:33:46 +08:00
|
|
|
Console.WriteLine("数组 arr = " + ToString(arr));
|
2023-11-26 23:18:44 +08:00
|
|
|
int[] nums = [1, 3, 2, 5, 4];
|
2023-10-08 01:33:46 +08:00
|
|
|
Console.WriteLine("数组 nums = " + ToString(nums));
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
|
|
// 随机访问
|
2023-10-08 01:33:46 +08:00
|
|
|
int randomNum = RandomAccess(nums);
|
2023-02-08 22:18:02 +08:00
|
|
|
Console.WriteLine("在 nums 中获取随机元素 " + randomNum);
|
|
|
|
|
|
|
|
// 长度扩展
|
2023-10-08 01:33:46 +08:00
|
|
|
nums = Extend(nums, 3);
|
|
|
|
Console.WriteLine("将数组长度扩展至 8 ,得到 nums = " + ToString(nums));
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
|
|
// 插入元素
|
2023-10-08 01:33:46 +08:00
|
|
|
Insert(nums, 6, 3);
|
|
|
|
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + ToString(nums));
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
|
|
// 删除元素
|
2023-10-08 01:33:46 +08:00
|
|
|
Remove(nums, 2);
|
|
|
|
Console.WriteLine("删除索引 2 处的元素,得到 nums = " + ToString(nums));
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
|
|
// 遍历数组
|
2023-10-08 01:33:46 +08:00
|
|
|
Traverse(nums);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
|
|
// 查找元素
|
2023-10-08 01:33:46 +08:00
|
|
|
int index = Find(nums, 3);
|
2023-02-08 22:18:02 +08:00
|
|
|
Console.WriteLine("在 nums 中查找元素 3 ,得到索引 = " + index);
|
2022-12-14 13:54:49 +08:00
|
|
|
}
|
2022-12-24 16:15:41 +08:00
|
|
|
}
|