2022-12-16 15:53:26 +08:00
|
|
|
// File: Array.cs
|
|
|
|
// Created Time: 2022-12-14
|
|
|
|
// Author: mingXta (1195669834@qq.com)
|
|
|
|
|
2022-12-14 23:04:56 +08:00
|
|
|
namespace hello_algo.chapter_array_and_linkedlist
|
2022-12-14 13:54:49 +08:00
|
|
|
{
|
|
|
|
public class Array
|
|
|
|
{
|
2022-12-16 15:53:26 +08:00
|
|
|
//随机返回一个数组元素
|
2022-12-14 13:54:49 +08:00
|
|
|
public static int RandomAccess(int[] nums)
|
|
|
|
{
|
|
|
|
Random random = new();
|
|
|
|
int randomIndex = random.Next(nums.Length);
|
|
|
|
int randomNum = nums[randomIndex];
|
|
|
|
return randomNum;
|
|
|
|
}
|
|
|
|
|
2022-12-16 15:53:26 +08:00
|
|
|
//扩展数组长度
|
2022-12-14 13:54:49 +08:00
|
|
|
public static 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;
|
|
|
|
}
|
|
|
|
|
2022-12-16 15:53:26 +08:00
|
|
|
//在数组的索引 index 处插入元素 num
|
2022-12-14 13:54:49 +08:00
|
|
|
public static 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;
|
|
|
|
}
|
|
|
|
|
2022-12-16 15:53:26 +08:00
|
|
|
//删除索引 index 处元素
|
2022-12-14 13:54:49 +08:00
|
|
|
public static void Remove(int[] nums, int index)
|
|
|
|
{
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
for (int i = index; i < nums.Length - 1; i++)
|
|
|
|
{
|
|
|
|
nums[i] = nums[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 15:53:26 +08:00
|
|
|
//遍历数组
|
2022-12-14 13:54:49 +08:00
|
|
|
public static void Traverse(int[] nums)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
// 通过索引遍历数组
|
|
|
|
for (int i = 0; i < nums.Length; i++)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
// 直接遍历数组
|
|
|
|
foreach (int num in nums)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 15:53:26 +08:00
|
|
|
//在数组中查找指定元素
|
2022-12-14 13:54:49 +08:00
|
|
|
public static int Find(int[] nums, int target)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < nums.Length; i++)
|
|
|
|
{
|
|
|
|
if (nums[i] == target)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-12-16 15:53:26 +08:00
|
|
|
//辅助函数,数组转字符串
|
2022-12-14 13:54:49 +08:00
|
|
|
public static string ToString(int[] nums)
|
|
|
|
{
|
|
|
|
return string.Join(",", nums);
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 15:53:26 +08:00
|
|
|
}
|