[英]Add array to jagged array
I want to create a dynamic jagged array with dynamic data. 我想用动态数据创建一个动态锯齿状数组。 The problem is that a part of the jagged array is only a two column type and the rest of it is a 4 column type. 问题在于锯齿状数组的一部分只是两列类型,其余部分是4列类型。 Code is in C#. 代码在C#中。
public static Object[][] my_array = new Object[20][];
public static void LoadData()
{
for(int i = 0; i < 20; i++)
{
my_array[i] = new Object[20];
my_array[i][0] = "Data1";
my_array[i][1] = "Data2";
my_array[i][2] = "Data3";
my_array[i][3] = "Data4";
my_array[i][4] = new Object[100];
for(int j = 0; j < 100; j++)
{
my_array[i][4][j] = new Object[200];
my_array[i][4][j][0] = "SubData1";
}
my_array[i][5] = "Data6";
}
}
I get following error: 我收到以下错误:
Severity Code Description Project File Line Suppression State Error CS0021 Cannot apply indexing with [] to an expression of type 'object' 严重性代码说明项目文件行抑制状态错误CS0021无法将带有[]的索引应用于类型'object'的表达式
Is this even possible to do this in C#? 在C#中甚至可以做到这一点吗?
您应在应用索引之前将其强制转换为Array,例如:
var array = (Object[]) my_array[i][4];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.