C# 语法练习(7): 数组
2009-02-23 08:16:57 来源:WEB开发网
using System;
class MyClass
{
static void Main()
{
int[,] arr = new int[3, 4];
arr[0,0] = 11;
arr[0,1] = 12;
arr[0,2] = 13;
arr[0,3] = 14;
arr[1,0] = 21;
arr[1,1] = 22;
arr[1,2] = 23;
arr[1,3] = 24;
arr[2,0] = 31;
arr[2,1] = 32;
arr[2,2] = 33;
arr[2,3] = 34;
foreach (int i in arr) Console.WriteLine(i);
Console.ReadKey();
}
}
多维数组:
using System;
class MyClass
{
static void Main()
{
int[,,] arr = new int[2, 3, 4];
for (int x = 0; x < 2; x++) for (int y = 0; y < 3; y++) for (int z = 0; z < 4; z++)
arr[x,y,z] = Convert.ToInt32(Convert.ToString(x+1) + Convert.ToString(y+1) + Convert.ToString(z+1));
foreach (int i in arr) Console.WriteLine(i);
Console.ReadKey();
}
}
数组中的数组:
using System;
class MyClass
{
static void Main()
{
int[][] arr = new int[3][];
arr[0] = new int[2] { 11, 12 };
arr[1] = new int[3] { 21, 22, 23 };
arr[2] = new int[4] { 31, 32, 33, 34 };
foreach (int[] ns in arr) foreach (int n in ns)
Console.WriteLine(n);
Console.ReadKey();
}
}
更多精彩
赞助商链接