定义方法1
int[,] twodim=new int[3,3];
twodim [0, 0] = 1;
twodim [0, 1] = 2;
twodim [0, 2] = 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Console.Write (twodim[i,j]+" ");
}
Console.WriteLine ();
}
int[,] arr = {
{4,8,9,6,0},
{7,4,22,11,4},
{90,8,65,32,1},
{12,5,2,8,0}
};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
Console.Write (arr[i,j]+" ");
}
Console.WriteLine ();
}
第二种方法
锯齿数组
int[][]twodim=new int[3][];
twodim [0] = new int[]{ 1, 2, 3 };
twodim [1] = new int[]{ 1, 2, 3, 4 };
twodim [2] = new int[]{ 1, 2, 3, 4, 5 };
for (int i = 0; i < twodim.Length; i++) {
for (int j = 0; j < twodim[i].Length; j++) {
Console.Write(twodim[i] [j]+" ");
}
Console.WriteLine ();
}
总结二
第一种声明格式,:int[,]遍历访问的时候采取arr[i,j]的方式进行访问
第二种声明方式:int【】【】 此种写法的二维数组实际上是由多个一维数组构成
列数,但是必须声明行数,访问方式是:arr[i][j];
练习一:采用int【,】方式进行声明数组并初始换,遍历数组元素
int [,]arr=new int[3,3];
arr [0, 0] = 1;
arr [0, 1] = 5;
arr [0, 2] = 4;
arr [1, 1] = 2;
arr [2, 2] = 6;
arr [1, 0] = 5;
arr [1,2] = 9;
arr [2,0 ] = 8;
arr [2, 1] = 10;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Console.Write (arr[i,j]+" ");
}
Console.WriteLine ();
}
练习2:采用int[][]方式进行声明并初始化,遍历数组元素
int[][] arr=new int[3][];
arr [0] = new int[]{3,4,5 };
arr [1] = new int[]{ 7, 8, 9, 10 };
arr [2] = new int[]{ 11, 12, 1, 3, 14,};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < arr[i].Length; j++) {
Console.Write (arr[i][j]+" ");
}
Console.WriteLine ();
}
练习3:有一个三行四列的二维数组,要求编程找出最大元素,并输出所在的行和列
int[,] arr = {
{4,8,9,6},
{7,4,22,11},
{90,8,65,32},
};
int max=0;
int max0=0;
int max1=0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j <4 ; j++) {
if (arr [i,j] > max) {
max=arr [i,j];
max0 = i;
max1 = j;
}
}
}
Console.Write ("最大值为: {0}坐标为:{1} {2}",max+" ",max0,max1);
}
Array.Reverse;
Array.Sort;
int[] arr={10,9,8,7,6};
//Array.Reverse (arr);
Array.Reverse(arr,0,2);
foreach (var item in arr) {
Console.WriteLine (item);
}
clone----会创建一个新的数组出来,他会在托管对开辟内存空间
int [] intOrignArray=new int[4];
intOrignArray [0] = 1;
intOrignArray [1] = 2;
intOrignArray [2] = 3;
intOrignArray [3] = 4;
演示clone方法
int [] newArray=intOrignArray.Clone();
intOrignArray [0] = 100;
//遍历newArray
foreach (var item in newArray) {
Console.WriteLine (item);
}
copy容器数组的长度必须大于或等于原数组的长度
(浅拷贝与深拷贝的区别)
int [] intOrignArray=new int[4];
int[] newArray = { 1, 2, 3, 4 };
newArray.CopyTo (intOrignArray, 0);
newArray [0] = 100;
foreach (var item in intOrignArray) {
Console.WriteLine (item);
}
1.(**)从控制台先输入你一个整数n,表示之后会输入n个年龄.
将结果按由大到小排序输出.例如:
请输入n:
3
请输入3个年龄:
28
31
19
结果为:
31,28,19
Console.WriteLine ("请输入n:");
int n = int.Parse (Console.ReadLine ());
Console.WriteLine ("请输入{0}个年龄:",n);
int []arr=new int[n];
for (int i = 0; i <n ; i++) {
arr [i] = int.Parse (Console.ReadLine ());
}
Array.Sort(arr);
Array.Reverse (arr);
foreach (var item in arr) {
Console.Write ("{0},",item);
}
2.(**)求一个4阶数字矩阵(数值随机产生)对角线的和,如;
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
输出: 69.
int sum=0;
int [,]arr=new int[4,4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j <4; j++) {
Thread.Sleep (10);
arr [i, j] =new Random ().Next (100);;
if (i == j||i+j==3) {
sum += arr [i, j];
}
}
}
for (int k = 0; k < 4; k++) {
for (int l = 0; l < 4; l++) {
Console.Write (arr[k,l]+" ");
}
Console.WriteLine ();
}
Console.WriteLine ("对角线的和为:{0}",sum);
3.(***)输入一个n,随机生成一个n*n的二维数组地图,数组元素值随机产生.完成如下操作.例如输入3,
自动生成:
1 2 3
4 5 6
7 8 9
int t=1;
int n=int.Parse(Console.ReadLine());
int [,]arr=new int[n,n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Thread.Sleep (10);
t=new Random ().Next (100);
arr [i, j]=t;
}
}
for (int k = 0; k < n; k++) {
for (int f = 0; f < n; f++) {
Console.Write (arr[k,f]+" ");
}
Console.WriteLine ();
}
1)求下三角元素的和.(上例为1+4+5+7+8+9=34)
int[,] arr = {
{1,2,3},
{4,5,6},
{7,8,9}
};
int sum = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++) {
if (i >= j)
{
sum += arr [i, j];
}
Console.Write (arr[i,j]+" ");
}
Console.WriteLine ();
}
Console.WriteLine ("三角元素的和为:{0}",sum);
2)遍历二维数组,如果二维数组元素值为偶数,将元素更新为’*’,如果为奇数,将元素更新为’ #’.
# * #
* # *
# * #
int[,] arr = {
{1,2,3,},
{4,5,6},
{7,8,9}
};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
{
if (arr [i, j] % 2 == 0) {
Console.Write ("* ");
}
else {
Console.Write ("# ");
}
}
Console.WriteLine ();
}
3)将上图看做一个游戏地图(#为二维坐标系0,0点),输入一个x,y值,将元素更改为’$’.
例如:输入(0,0),输出
# * #
* # *
$ * #
int[,] arr = {
{1,2,3,},
{4,5,6},
{7,8,9}
};
int a = int.Parse (Console.ReadLine ());
int b = int.Parse (Console.ReadLine ());
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
{
if (arr [i, j] % 2 == 0) {
if (i == 3-b-1&&j==a) {
Console.Write ("$ ");
} else {
Console.Write ("* ");
}
}
else if(arr [i, j] % 2 != 0) {
if (i == 3-b-1&&j==a) {
Console.Write ("$ ");
} else {
Console.Write ("# ");
}
}
}
Console.WriteLine ();
}
// 5、(****)输入n(n < 10),代表矩阵n*n,输出蛇形矩阵。
// 例如:n = 3时,输出:
// 1 2 3
// 8 9 4
// 7 6 5
// n = 4时,输出:
// 1 2 3 4
// 12 13 14 5
// 11 16 15 6
// 10 9 8 7
//
int n =6;
int temp = 1;
int x = 0;//行上边
int y =0;//行下边
int a = 0;//列右边
int b = 0;//列左边
int[,] intArray = new int[n,n];
for (int k = 1; k < n*n; k ++) {
if ( x <= n/2) { //一行
for (int i = b; i < n - a ; i++) {
intArray [x, i] = temp;
temp++;
}
x++;
}
if ( a <= n /2) {//一列
for (int i = x; i < n - y ; i++) {
intArray [i, n -1 -a] = temp;
temp++;
}
a++;
}
if ( y <= n / 2) {//二行
for (int i = n -1 - a ; i >= b; i--) {
intArray [n - 1 - y, i] = temp;
temp++;
}
y++;
}
if ( b <= n / 2) {//二列
for (int i = n - y -1 ; i >= x; i--) {
intArray [i, b] = temp;
temp++;
}
b++;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Console.Write (intArray [i,j]+ "\t");
}
Console.WriteLine ();
}