gpt4 book ai didi

c# - 使用设置的数组长度初始化数组数组

转载 作者:太空宇宙 更新时间:2023-11-03 21:25:10 24 4
gpt4 key购买 nike

我想像在 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 };

而且,显然,您可以实现一种 forforeach 循环,以便从某些数据结构填充数组。更多阅读 Material 请访问:http://msdn.microsoft.com/en-us/library/2s05feca.aspx

此外,您可能应该考虑使用多维数组,例如 int[,](本例中的 C# 语法不同于 Java lang)。希望这会有所帮助。

关于c# - 使用设置的数组长度初始化数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27496479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com