C#笔记09 数组
——本系列是基于人民邮电出版社《C#2008 C#图解教程》、清华大学出版社《C#入门经典(第五版)》两本书的自学C#笔记,如果您发现了本文的纰漏,还望不吝指正。
写在前边
*复习:类型是一种模版,C#提供了15种预定义类型,还可以创建自己的用户定义类型,有6种类型:
- 类类型(class)
- 结构类型(struct)
- 数组类型(array)
- 枚举类型(enum)
- 委托类型(delegate)
- 接口类型(interface)
1. 什么是数组
数组实际上是由一个变量名称表示的一组同类型的数据元素。每个元素通过变量名称和一个或多个方括号中的索引名称来访问———人民邮电出版社《C#2008 C#图解教程》
数组是一个变量的索引列表,存储在数组类型的变量中。
数组一旦被创建,大小就固定了。C#不支持动态数组。
2. 数组的类型
2.1 一维数组:单行元素或元素向量
int[] Array = new int[10];
2.2 多维数组:包括多维数组和交错数组
1).矩形数组:
- 某个维度的所有子数组有相同长度的多维数组。
- 不管有多少维度,总是使用一组方括号。
int x = myArray[4,6,1] //使用一组方括号
——上图出自 人民邮电出版社《C#2008 C#图解教程》第14章
2).交错数组
- 每一个子数组都是独立数组的多维度数组。
- 可以有不同长度的子数组。
- 为数组的每一个维度使用一对方括号。
jagArray[2][7][4] //使用三组方括号
3. 数组的声明和实例化
数据类型[] 数组名;
3.1 一维数组
- 数据类型[] 数组名=new 数据类型[长度];
- 数据类型[] 数组名={ 元素1,元素2,元素3… };
- 数据类型[] 数组名=new 数据类型[长度]{ 元素1,元素2,元素3… };
int []arr = new int[4];
int []arr = {0,1,2,3};
int []arr = new int[4]{0,1,2,3};
3.2 二维数组
- 数据类型[,] 数组名=new 数据类型[行的长度,列的长度]
- 数据类型[,] 数组名=初始值
int[,] arr= new int[2,3] // 2行3列
int[,] arr={ {1,2,3} ,{4,5,6} };
3.3 交错数组
交错数组的元素是一个一维数组
- 数据类型[] [] 数组名 = new 数据类型 [交错数组的长度] [ ]
int[][] arr = new int[3][]; //交错数组的长度为3,也就是有三个一维数组
int[][] arr = { new int[] { 1, 2, 3 } ,new int[] { 1 } ,new int[] { 1, 2 } };
int[][] arr = new int [3] [] { new int[] { 1, 2, 3 } ,new int[] { 1 } ,new int[] { 1, 2 } };
4. 数组元素的访问与赋值
数组实例是从System.Array继承的对象。由于数组从BCL基类继承,它们也继承了很多有用的方法,
- .Length:返回数组的长度(数组中所有元素的个数)的属性
- .GetLength( index ) ,返回数组的指定维度的长度,从0开始,0代表第一个维度,1代表第二个维度…
4.1 一维数组
1).通过下标号去访问 数组名[下标号]
int[] A = new int[10];
int A[2] = 10; //向第三个元素写入值
for (int i = 0; i < A.Length; i++)
{
A[i] = i; //创建一个数组A,值与下标一样
}
2).for 循环遍历数组
string[] friendNames = {"Robert Barwell", "Mike Parry","Jeremy Beacock"};
for (int i = 0; i < friendNames.Length; i++)
{
Console.WriteLine(friendNames[i]);
}
3).foreach循环遍历数组
string[] friendNames = { "Robert Barwell", "Mike Parry","Jeremy Beacock" };
foreach (string friendName in friendNames)
{
Console.WriteLine(friendName);
}
foreach循环对数组内容进行只读访问,所以不能改变任何元素的值
4.2 二维数组
1).二维数组遍历
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j= 0; j< arr.GetLength(1); j++)
{
Console.WriteLine(arr[i,j]); //arr[i,j] i:代表在哪一个数组中,j:代表在该数组中的第几位
}
}
4.3 交错数组
1).交错数组赋值
int[][] arr = new int[3][]; //交错数组的长度为3,也就是有三个一维数组
arr[0] = new int[] { 1, 2, 3, 4, 5 };
arr[1] = new int[] { 6, 7, 8 };
arr[2] = new int[] { 9, 10 };
2).交错数组遍历
for( int i = 0; i < arr.GetLength(0); i++) //获取行号
{
for(int j = 0; j < arr[i].GetLength(0); j++) //每一行都是一个一维数组
{
Console.Write(arr[i][j]); //为第i个数组的第j个元素赋值
}
Console.WriteLine();
}
foreach (int[] array in arr)
{
foreach (int item in array)
{
Console.Write(item);
}
Console.WriteLine("");
}
5. 比较三种数组类型
——上表出自 人民邮电出版社《C#2008 C#图解教程》第14章
写在最后(扩展学习)
1. 冒泡排序
for (int i = 0; i < arr.Length; i++)//i代表总共比较了多少次
{
for (int j = 0; j < arr.Length-1-i; j++)
{
if (arr[j]>arr[j+1])//两两比较,大的往后放
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
2. foreach语句和多维数组
1).矩形数组
如下代码演示了foreach语句用于矩形数组:
int total = 0;
int[,] arr = { { 1, 2 },{ 10,11 } };
foreach (var item in arr)
{
total += item;
Console.WriteLine( "item:{0}, Current Total:{1}",item,total);
}
控制台输出结果:
item:1, Current Total:1
item:2, Current Total:3
item:10, Current Total:13
item:11, Current Total:24
2).交错数组
交错数组中的每一个维度使用独立的foreach语句。
foreach语句必须嵌套以确保每一个嵌套数组都被正确处理。
如下代码演示了foreach语句用于交错数组:
int total = 0;
int[][] arr = new int[2][];
arr[0] = new int[] { 10, 11 };
arr[1] = new int[] { 12, 13, 14 };
foreach (int[] array in arr) //处理顶层数组
{
Console.WriteLine("Starting new array");
foreach (var item in array) //处理第二次数组
{
total += item;
Console.WriteLine(" item:{0}, Current Total:{1}", item, total);
}
}
控制台输出结果:
Starting new array
item:10, Current Total:10
item:11, Current Total:21
Starting new array
item:12, Current Total:33
item:13, Current Total:46
item:14, Current Total:60
3. 数组的属性和方法扩展
C#数组从System.Array类继承。它们可以从基类继承很多有用的属性和方法
——上表出自 人民邮电出版社《C#2008 C#图解教程》第14章
例如,下面的代码使用了其中的一些属性和方法:
*例子中存在一些学习到现阶段看不懂的知识点,对比之后关于C#类的笔记进行学习
public static void PrintArry(int[] a)
{
foreach (var item in a)
{
Console.Write("{0}", item);
}
Console.WriteLine();
}
static void Main()
{
int[] arr = new int[] { 3 , 1 , 2 , 7 , 8 , 6 };
PrintArry(arr);
Array.Sort(arr); //排序
PrintArry(arr);
Array.Reverse(arr); //反转
PrintArry(arr);
Console.WriteLine("(分割线)");
Console.WriteLine("Rank = {0} , Length = {1}",arr.Rank,arr.Length);
Console.WriteLine("GetLength(0) = {0}",arr.GetLength(0));
Console.WriteLine("GetType() = {0}",arr.GetType());
}
//代码源自 人民邮电出版社《C#2008 C#图解教程》第14章
控制台输出结果:
312786
123678
876321
(分割线)
Rank = 1 , Length = 6
GetLength(0) = 6
GetType() = System.Int32[]