1. 数组的基本概念
数组是一种数据结构,用于存储相同类型的多个元素。在C#中,数组是引用类型,它有以下特点:
- 数组中的所有元素必须是相同类型
- 数组的长度在创建时确定,之后不能更改
- 数组的索引从0开始
- 数组可以存储基本类型和引用类型
2. 一维数组的声明
在C#中,声明一维数组有以下几种方式:
2.1 基本声明方式
示例:声明一维数组
// 语法:数据类型[] 数组名;
// 声明整型数组
int[] numbers;
// 声明字符串数组
string[] names;
// 声明双精度浮点型数组
double[] prices;
// 声明布尔型数组
bool[] flags;
2.2 声明并初始化
示例:声明并初始化一维数组
// 声明并初始化整型数组
int[] numbers = new int[5]; // 创建长度为5的整型数组
// 声明并初始化字符串数组
string[] names = new string[3]; // 创建长度为3的字符串数组
3. 一维数组的初始化
在C#中,一维数组的初始化有多种方式:
3.1 使用new关键字初始化
示例:使用new关键字初始化数组
// 方式1:指定长度,元素为默认值
int[] numbers = new int[5]; // 元素初始化为0
// 方式2:指定长度并初始化元素
int[] numbers = new int[5] { 1, 2, 3, 4, 5 };
// 方式3:不指定长度,由初始化列表确定
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
// 方式4:简化语法,不使用new关键字
int[] numbers = { 1, 2, 3, 4, 5 };
3.2 不同类型数组的初始化
示例:不同类型数组的初始化
// 整型数组
int[] intArray = { 1, 2, 3, 4, 5 };
// 浮点型数组
double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5 };
// 字符串数组
string[] stringArray = { "张三", "李四", "王五" };
// 布尔型数组
bool[] boolArray = { true, false, true };
// 字符型数组
char[] charArray = { 'a', 'b', 'c', 'd' };
// 对象数组
object[] objectArray = { 1, "Hello", 3.14, true };
3.3 动态初始化
示例:动态初始化数组
// 根据用户输入确定数组长度
Console.WriteLine("请输入数组长度:");
int length = int.Parse(Console.ReadLine());
// 创建指定长度的数组
int[] numbers = new int[length];
// 动态赋值
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine($"请输入第{i + 1}个元素:");
numbers[i] = int.Parse(Console.ReadLine());
}
// 显示数组元素
Console.WriteLine("数组元素:");
foreach (int number in numbers)
{
Console.WriteLine(number);
}
4. 数组的默认值
当创建数组但未初始化元素时,数组元素会被赋予默认值。不同类型的默认值如下:
| 数据类型 | 默认值 |
|---|---|
| byte, sbyte, short, ushort, int, uint, long, ulong | 0 |
| float, double, decimal | 0.0 |
| bool | false |
| char | '\0'(空字符) |
| string | null |
| 引用类型 | null |
示例:数组的默认值
// 创建不同类型的数组,查看默认值
int[] intArray = new int[3];
Console.WriteLine("整型数组默认值:" + intArray[0]); // 输出:0
double[] doubleArray = new double[3];
Console.WriteLine("双精度数组默认值:" + doubleArray[0]); // 输出:0
bool[] boolArray = new bool[3];
Console.WriteLine("布尔数组默认值:" + boolArray[0]); // 输出:False
string[] stringArray = new string[3];
Console.WriteLine("字符串数组默认值:" + stringArray[0]); // 输出:
object[] objectArray = new object[3];
Console.WriteLine("对象数组默认值:" + objectArray[0]); // 输出:
5. 数组的长度
在C#中,可以通过数组的Length属性获取数组的长度:
示例:获取数组长度
// 创建数组
int[] numbers = { 1, 2, 3, 4, 5 };
// 获取数组长度
int length = numbers.Length;
Console.WriteLine("数组长度:" + length); // 输出:5
// 使用长度遍历数组
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine($"numbers[{i}] = {numbers[i]}");
}
6. 数组的访问和修改
在C#中,可以通过索引访问和修改数组元素:
示例:访问和修改数组元素
// 创建并初始化数组
int[] numbers = { 10, 20, 30, 40, 50 };
// 访问数组元素
Console.WriteLine("第一个元素:" + numbers[0]); // 输出:10
Console.WriteLine("第三个元素:" + numbers[2]); // 输出:30
// 修改数组元素
numbers[1] = 25;
Console.WriteLine("修改后的第二个元素:" + numbers[1]); // 输出:25
// 尝试访问超出范围的索引(会抛出异常)
// Console.WriteLine(numbers[10]); // 运行时错误:索引超出范围
7. 数组的遍历
在C#中,有多种方式遍历数组:
7.1 使用for循环遍历
示例:使用for循环遍历数组
int[] numbers = { 1, 2, 3, 4, 5 };
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine($"numbers[{i}] = {numbers[i]}");
}
7.2 使用foreach循环遍历
示例:使用foreach循环遍历数组
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}
7.3 使用while循环遍历
示例:使用while循环遍历数组
int[] numbers = { 1, 2, 3, 4, 5 };
int i = 0;
while (i < numbers.Length)
{
Console.WriteLine($"numbers[{i}] = {numbers[i]}");
i++;
}
8. 数组的常见错误和注意事项
8.1 索引越界
注意:索引越界
当尝试访问超出数组范围的索引时,会抛出IndexOutOfRangeException异常。
int[] numbers = { 1, 2, 3 };
// 错误:索引越界
Console.WriteLine(numbers[5]); // 运行时异常
8.2 数组长度不可变
注意:数组长度不可变
数组的长度在创建时确定,之后不能更改。如果需要动态调整大小,应该使用List<T>等集合类型。
int[] numbers = { 1, 2, 3 };
// 错误:不能直接修改数组长度
// numbers.Length = 5; // 编译错误
8.3 数组是引用类型
注意:数组是引用类型
数组是引用类型,当将一个数组赋值给另一个数组变量时,它们指向同一个内存地址。
int[] array1 = { 1, 2, 3 };
int[] array2 = array1;
// 修改array2会影响array1
array2[0] = 100;
Console.WriteLine(array1[0]); // 输出:100
9. 完整示例程序
示例:一维数组的综合应用
using System;
namespace ArrayExample
{
class Program
{
static void Main(string[] args)
{
// 1. 声明和初始化数组
int[] numbers = { 10, 20, 30, 40, 50 };
string[] names = new string[3] { "张三", "李四", "王五" };
double[] scores = new double[4]; // 默认初始化为0
// 2. 访问和修改数组元素
Console.WriteLine("原始数组:");
Console.WriteLine("numbers[0] = " + numbers[0]);
Console.WriteLine("names[1] = " + names[1]);
Console.WriteLine("scores[0] = " + scores[0]);
// 修改数组元素
numbers[0] = 100;
names[1] = "赵六";
scores[0] = 95.5;
Console.WriteLine("\n修改后:");
Console.WriteLine("numbers[0] = " + numbers[0]);
Console.WriteLine("names[1] = " + names[1]);
Console.WriteLine("scores[0] = " + scores[0]);
// 3. 获取数组长度
Console.WriteLine("\n数组长度:");
Console.WriteLine("numbers.Length = " + numbers.Length);
Console.WriteLine("names.Length = " + names.Length);
Console.WriteLine("scores.Length = " + scores.Length);
// 4. 遍历数组
Console.WriteLine("\n遍历numbers数组(for循环):");
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine($"numbers[{i}] = {numbers[i]}");
}
Console.WriteLine("\n遍历names数组(foreach循环):");
foreach (string name in names)
{
Console.WriteLine(name);
}
// 5. 动态初始化数组
Console.WriteLine("\n动态初始化数组:");
Console.Write("请输入数组长度:");
int length = int.Parse(Console.ReadLine());
int[] dynamicArray = new int[length];
for (int i = 0; i < dynamicArray.Length; i++)
{
Console.Write($"请输入第{i + 1}个元素:");
dynamicArray[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("\n动态数组元素:");
foreach (int num in dynamicArray)
{
Console.WriteLine(num);
}
// 6. 数组的默认值
Console.WriteLine("\n数组的默认值:");
int[] defaultIntArray = new int[3];
bool[] defaultBoolArray = new bool[3];
string[] defaultStringArray = new string[3];
Console.WriteLine("int数组默认值:" + defaultIntArray[0]);
Console.WriteLine("bool数组默认值:" + defaultBoolArray[0]);
Console.WriteLine("string数组默认值:" + (defaultStringArray[0] ?? "null"));
Console.WriteLine("\n按任意键退出...");
Console.ReadKey();
}
}
}
10. 总结
通过本教程的学习,你应该掌握了C#中一维数组的声明与初始化方法:
- 数组的基本概念和特点
- 一维数组的声明方式
- 一维数组的初始化方法(使用new关键字、简化语法等)
- 不同类型数组的初始化
- 数组的默认值
- 数组的长度获取
- 数组的访问和修改
- 数组的遍历方法(for、foreach、while循环)
- 数组的常见错误和注意事项
数组是C#中非常重要的数据结构,掌握好数组的使用方法,对于开发各种应用程序都非常重要。在实际开发中,你可以根据具体需求,选择合适的数组类型和初始化方式,来存储和处理数据。