class ArrayTest { static void Main() { // Declare the array of two elements: int[][] arr = new int[2][]; // Initialize the elements: arr[0] = new int[5] { 1, 3, 5, 7, 9 }; arr[1] = new int[4] { 2, 4, 6, 8 }; // Display the array elements: for (int i = 0; i < arr.Length; i++) { System.Console.Write("Element({0}): ", i); for (int j = 0; j < arr[i].Length; j++) { System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " "); } System.Console.WriteLine(); } } }
for (int i = 0; i < a.GetLength(0); i++) { if (a[i, 0] != null) { .... } } a.GetLength(0)= 2; a.GetLength(1)= 3; 具体你应该知道怎么做了吧!
多维数组 int a[2,3]; 我要使用a的第1行,应该怎么办? for(int i=0;i<1;i++) { for(int j =0;j<3;j++) { Console.WriteLine(a[i,j].ToString()); } }数组的数组 int a[2][3] 我要使用foreach,应该怎么办?foreach(int[] aa in a) { foreach(int aa1 in aa) { Console.WriteLine(aa1.ToString()); } }
{
}
a[0][]
{
if (a[i, 0] != null)
{
....
}
}
a.GetLength(0)= 2;
a.GetLength(1)= 3;
具体你应该知道怎么做了吧!
http://msdn.microsoft.com/zh-cn/library/2s05feca(VS.80).aspx
{
static void Main()
{
// Declare the array of two elements:
int[][] arr = new int[2][]; // Initialize the elements:
arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 }; // Display the array elements:
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write("Element({0}): ", i); for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
}
System.Console.WriteLine();
}
}
}
{
if (a[i, 0] != null)
{
....
}
}
a.GetLength(0)= 2;
a.GetLength(1)= 3;
具体你应该知道怎么做了吧!
我要使用a的第1行,应该怎么办?
for(int i=0;i<1;i++)
{
for(int j =0;j<3;j++)
{
Console.WriteLine(a[i,j].ToString());
}
}数组的数组 int a[2][3]
我要使用foreach,应该怎么办?foreach(int[] aa in a)
{
foreach(int aa1 in aa)
{
Console.WriteLine(aa1.ToString());
}
}
我要
a[2,3]的
a[0,0],a[0,1],a[0,2]
一定要循环每个都取出来?
而不能用诸如a[0]就把这行选出来了。第二个问题其实是觉得这个用法很烦。
1、必须的,这不是C++,所以这是必须的。
2、麻烦的,这不是C++,所以这是麻烦的。谁让你用C#来着。
你可能是想比较一下int[,]和int[][]哪个好用罢了……
矩形阵就用前者,邻接阵就用后者……想用C#就不要去考虑“技巧”,那是给C++和汇编程序员考虑的。C#的灵活可不是在这上面,C#的语言模型是设计模式,不是二进制……