一、为什么要使用多维数组
生活中,有很多事物,仅仅用一维数组,将无法恰当地被表示。还是说学生成绩管理吧。一个班级30个学员,你把他们编成1到30号,这很好。但现在有两个班级要管理怎么办?人家每个班级都自有自的编号,比如一班学生编是1~30;二班的学生也是1~30。你说,不行,要进行计算机管理,你们两班学员的编号要混在一起,从1号编到60号。
另外一种情况,仍然只有一个班级30人。但这回他们站到了操场,他们要做广播体操,排成5行6列。这时所有老师都不管学员的编号了,老师会这样喊:“第2排第4个同学,就说你啦!踢错脚了!”。假设我们的校长大人要坐在校长室里,通过一个装有监视器的电脑查看全校学员做广播体操,这时,我们也需要一个多维数组。
二、二维数组基本语法
语法:定义一个二维数组。
数据类型 数组名=new 数据类型[第二维大小][第一维大小];
举例:
int arr=new int[5][6]; //注意,以分号结束。
这就是操场上那个“5行6列的学生阵”。当然,哪个是行哪个列凭你的习惯。如果数人头时,喜欢一列一列地数,那你也可以当成它是“5列6行”——台湾人好像有这怪僻——我们还是把它看成5行6列吧。
现在:
第一排第一个学员是哪个?答:arr[0][0];
第二排第三个学员是?答:arr[1][2];
也不并不困难,对不?惟一别扭的其实还是那个老问题:现实上很多东西都是从1开始计数,而在C#里,总是要从0开始计数。
接下来,校长说,第一排的全体做得很好啊,他们的广播体操得分全部加上5分!程序如何写?答:
for(int col=0; col<6; col++)
{
arr[0][col] += 5;
}
对了,这里我没有用 i 来作循环的增量,而是用col。因为col在英语里表示“列”,这样更直观对不?下面要用到行,则用row。
广播操做到“跳跃运动”了,校长大人在办公室蹦了两下,感觉自已青春依旧,大为开心,决定给所有学员都加1分,程序如何写?答:
for(int row = 0; row < 5; row++)
{
for(int col = 0; col < 6; col++)
{
arr[row][col] += 1;
}
}
看明白了吗?在二维数组,要确定一个元素,必须使用两个下标。
另外,这个例子也演示了如何遍历一个二维数组:使用双层循环。第一层循环让row 从 0到 4, 用于遍历每一行;col从0到5,遍历每一行中的每一列。
(遍历:访问某一集合中的每一个元素的过程)
大家把这两个程序都实际试一试.
三、二维数组初始化
int arr[5][6] =
{
{ 0, 1, 2, 3, 4, 5},
{10,11,12,13,14,15},
{20,21,22,23,24,25},
{30,31,32,33,34,35},
{40,41,42,43,44,45},
}; //注意,同样以分号结束
初始化二维数组使用了两层{},内层初始化第一维,每个内层之间用逗号分隔。
例:
我们可以把这个数组通过双层循环输出:
for(int row = 0; row < 5; row++)
{
for(int col = 0; col < 6; col++)
{
Console.WriteLine("{0} ",arr[row][col]);
}
}
这段代码会把二维数组arr中的所有元素(5*6=30个),一行一个地,一古脑地输出,并不适于我们了解它的二维结构。我们在输出上做些修饰:
for(int row = 0; row < 5; row++)
{
Console.WriteLine("第" +( row + 1).ToStirng()+ "行: ");
for(int col = 0; col < 6; col++)
{
Console.WriteLine("{0} ",arr[row][col] ); //同一行的元素用逗号分开
}
Console.WriteLine(); //换行
}
请大家分别上机试验这两段代码,对比输出结果,明白二维数组中各元素次序。下面是完整程序中,后一段代码的输出:
四、综合举例
1.两个二给数组相加
using System;
using System.Collections.Generic;
using System.Text;
namespace ListArrayApp
{
class IIArray
{
int[,] aArray = new int[3, 3];
int[,] bArray = new int[3, 3];
int[,] cArray = new int[3, 3];
//接受
public void AcceptValue()
{
Console.WriteLine("请输入第一个数组的值:");
for (int i = 0; i < aArray.GetLength(0); i++)
{
for (int j = 0; j < aArray.GetLength(1); j++)
{
aArray[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine();
Console.WriteLine("请输入二个数组的值:");
for (int i = 0; i < bArray.GetLength(0); i++)
{
for (int j = 0; j < bArray.GetLength(1); j++)
{
bArray[i, j] = int.Parse(Console.ReadLine());
}
}
}
//计算
public void AddValue()
{
for (int i = 0; i < aArray.GetLength(0); i++)
{
for (int j = 0; j < aArray.GetLength(1); j++)
{
cArray[i, j] = aArray[i, j];
}
}
for (int i = 0; i < bArray.GetLength(0); i++)
{
for (int j = 0; j < bArray.GetLength(1); j++)
{
cArray[i, j] += bArray[i, j];
}
}
}
//输出
public void DisplayValue()
{
Console.WriteLine("第一个数组如下:");
for (int i = 0; i < aArray.GetLength(0); i++)
{
for (int j = 0; j < aArray.GetLength(1); j++)
{
Console.Write("{0} ", aArray[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("第二个数组如下:");
for (int i = 0; i < bArray.GetLength(0); i++)
{
for (int j = 0; j < bArray.GetLength(1); j++)
{
Console.Write("{0} ", bArray[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("计算结果数组如下:");
for (int i = 0; i < cArray.GetLength(0); i++)
{
for (int j = 0; j < cArray.GetLength(1); j++)
{
Console.Write("{0} ", cArray[i, j]);
}
Console.WriteLine();
}
}
}
class Program
{
static void Main(string[] args)
{
IIArray sa = new IIArray();
sa.AcceptValue();
sa.AddValue();
sa.DisplayValue();
Console.ReadLine();
}
}
}
2.求杨辉三角(又称Pascal三角)
using System;
using System.Collections.Generic;
using System.Text;
namespace IIIArrayApp
{
class Program
{
static void Main(string[] args)
{
int[,] sArray = new int[10, 10];
sArray[0, 0] = 1;
for (int i = 1; i <sArray.GetLength(0); i++)
{
sArray[i, 0] = 1;
sArray[i, i] = 1;
for (int j = 1; j <=i; j++)