我想像在 java 中一样初始化一个数组数组:
int[][] arrPos=new int[16][48];
int[][] arrPosOther=new int[16][48];
我可以像这样设置行数组值:
arrPos[0]=arrPosOther[0];
我可以像这样设置单元格值:
arrPos[1][0]=125;
但在 C# 中,我只能这样声明:
int[][] arrPos=new int[16][];
不能在初始化时设置列值。
请您参考如下方法:
看来您正在尝试找到一种在 C# 中初始化 Jagged 数组的方法:请引用以下示例:
int[][] jaggedArray2 = new int[][]
{
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}
};
同一样本的缩写形式如下所示:
int[][] jaggedArray2 =
{
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}
};
您还可以分几步执行初始化:
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
而且,显然,您可以实现一种 for 或 foreach 循环,以便从某些数据结构填充数组。更多阅读 Material 请访问:http://msdn.microsoft.com/en-us/library/2s05feca.aspx
此外,您可能应该考虑使用多维数组,例如 int[,](本例中的 C# 语法不同于 Java lang)。 希望这会有所帮助。
