C# 数组

7.1 一维数组

声明与初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 声明数组
int[] numbers;

// 声明并初始化
int[] numbers = new int[5]; // 5 个元素,默认值为 0

// 声明并赋值
int[] scores = new int[] { 85, 90, 78, 92, 88 };

// 简化写法
int[] values = { 1, 2, 3, 4, 5 };

// 使用 var
var items = new[] { "apple", "banana", "orange" };

访问元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int[] numbers = { 10, 20, 30, 40, 50 };

// 索引访问(从 0 开始)
Console.WriteLine(numbers[0]); // 10
Console.WriteLine(numbers[2]); // 30

// 修改元素
numbers[1] = 25;

// 数组长度
Console.WriteLine(numbers.Length); // 5

// 最后一个元素
Console.WriteLine(numbers[numbers.Length - 1]); // 50

7.2 遍历数组

for 循环

1
2
3
4
5
6
int[] numbers = { 10, 20, 30, 40, 50 };

for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine($"索引 {i}: {numbers[i]}");
}

foreach 循环

1
2
3
4
foreach (int num in numbers)
{
Console.WriteLine(num);
}

Array.ForEach

1
Array.ForEach(numbers, n => Console.WriteLine(n));

7.3 多维数组

二维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 声明 3x3 数组
int[,] matrix = new int[3, 3];

// 初始化
int[,] grid = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};

// 访问元素
Console.WriteLine(grid[0, 0]); // 1
Console.WriteLine(grid[1, 2]); // 6

// 遍历
for (int i = 0; i < grid.GetLength(0); i++)
{
for (int j = 0; j < grid.GetLength(1); j++)
{
Console.Write($"{grid[i, j]} ");
}
Console.WriteLine();
}

三维数组

1
2
int[,,] cube = new int[3, 3, 3];
cube[0, 0, 0] = 1;

7.4 交错数组

交错数组是”数组的数组”,每行长度可以不同。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 声明交错数组
int[][] jagged = new int[3][];

// 初始化每行
jagged[0] = new int[] { 1, 2 };
jagged[1] = new int[] { 3, 4, 5 };
jagged[2] = new int[] { 6, 7, 8, 9 };

// 简化写法
int[][] matrix = new int[][]
{
new int[] { 1, 2 },
new int[] { 3, 4, 5 },
new int[] { 6, 7, 8, 9 }
};

// 遍历
foreach (int[] row in matrix)
{
foreach (int num in row)
{
Console.Write($"{num} ");
}
Console.WriteLine();
}

7.5 Array 类

常用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int[] numbers = { 5, 2, 8, 1, 9, 3 };

// 排序
Array.Sort(numbers);
Console.WriteLine(string.Join(", ", numbers)); // 1, 2, 3, 5, 8, 9

// 反转
Array.Reverse(numbers);
Console.WriteLine(string.Join(", ", numbers)); // 9, 8, 5, 3, 2, 1

// 查找
int index = Array.IndexOf(numbers, 5); // 2
bool exists = Array.Exists(numbers, n => n > 7); // True

// 复制
int[] copy = new int[numbers.Length];
Array.Copy(numbers, copy, numbers.Length);

// 清空
Array.Clear(numbers); // 所有元素设为 0

调整大小

1
2
3
int[] arr = { 1, 2, 3 };
Array.Resize(ref arr, 5); // 扩容到 5
// arr: { 1, 2, 3, 0, 0 }

7.6 数组常用操作

查找最大值/最小值

1
2
3
4
5
6
7
8
9
10
11
12
13
int[] numbers = { 5, 2, 8, 1, 9, 3 };

int max = numbers[0];
int min = numbers[0];

foreach (int num in numbers)
{
if (num > max) max = num;
if (num < min) min = num;
}

Console.WriteLine($"最大值: {max}"); // 9
Console.WriteLine($"最小值: {min}"); // 1

求和与平均

1
2
3
4
5
6
7
8
9
10
11
12
int[] scores = { 85, 90, 78, 92, 88 };

int sum = 0;
foreach (int score in scores)
{
sum += score;
}

double average = (double)sum / scores.Length;

Console.WriteLine($"总和: {sum}"); // 433
Console.WriteLine($"平均: {average:F1}"); // 86.6

7.7 参数数组

params 关键字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 可变参数方法
public static int Sum(params int[] numbers)
{
int total = 0;
foreach (int n in numbers)
{
total += n;
}
return total;
}

// 调用
Console.WriteLine(Sum(1, 2, 3)); // 6
Console.WriteLine(Sum(1, 2, 3, 4, 5)); // 15
Console.WriteLine(Sum(new[] { 10, 20 })); // 30

7.8 数组与内存

值类型数组

1
2
3
4
int[] a = { 1, 2, 3 };
int[] b = a; // 引用相同数组
b[0] = 100;
Console.WriteLine(a[0]); // 100

复制数组

1
2
3
4
5
6
7
8
9
10
11
int[] original = { 1, 2, 3 };

// 浅拷贝
int[] copy1 = original.Clone() as int[];

// 复制
int[] copy2 = new int[original.Length];
Array.Copy(original, copy2, original.Length);

copy1[0] = 100;
Console.WriteLine(original[0]); // 1(不受影响)

7.9 本章小结

  • 数组是固定大小的同类型元素集合
  • 索引从 0 开始,使用 Length 获取长度
  • 多维数组:int[,],交错数组:int[][]
  • Array 类提供排序、反转、查找等方法
  • params 关键字支持可变参数
  • 数组是引用类型,赋值时复制引用

练习题

  1. 创建数组存储 5 个学生成绩,计算平均分
  2. 实现数组元素反转(不使用 Array.Reverse)
  3. 查找数组中第二大的数
  4. 以下代码输出什么?
    1
    2
    3
    4
    int[] a = { 1, 2, 3 };
    int[] b = a;
    b[0] = 10;
    Console.WriteLine(a[0]);
ByteFisher
分享编程技术 · 记录钓鱼乐趣
扫码关注
▸ 扫码关注 ◂
C#基础教程 系列
第 7/20 篇
分享: