【问题标题】:How do I insert dynamic string values to my multi dimentional array in c#如何在 C# 中将动态字符串值插入到我的多维数组中
【发布时间】:2012-06-17 02:24:12
【问题描述】:

我正在尝试创建一个列的长度是动态的多维字符串数组。

行数和列数各不相同,但我似乎无法将其存储在具有如下输出的变量中:

index[0] = {"string1", "string2", "string" and so on..}
index[1] = {"string1", "string2", "string" and so on..}

这是我的代码:

int arrayRows = itemCollection.Count;
int arrayColumns = parsedColumns.Count;


 String[,] listDataArray = new String[arrayRows, arrayColumns];
 for (int i = 0; i != arrayRows; i++)
 {
   for (int j = 0; j != arrayColumns; j++)
     {
       listDataArray[i, j] = "" + itemCollection[i][parsedColumns[j]];           
     }
 }

非常感谢您提前提供的帮助! :)

【问题讨论】:

  • 您能否包含您的 itemCollection,以便我们查看您正在处理的内容?
  • 我认为你想要一个锯齿状数组,而不是多维数组(即 String[][])
  • 您好,感谢您的回复。 itemCollection 是一个 SPListItemCollection(共享点列表)。 parsedColumns 是 SPList 中可用的列。我必须在不强烈输入列名的情况下获取每列的值(因为它会有所不同),这就是我尝试这样做的原因。 :)
  • 你可以使用 foreach(SPListItem item in itemCollection) msdn.microsoft.com/en-us/library/ms457534.aspx
  • 嗨,Jake,这是我首先做的,但是在检索 itemcollection 中的每个项目时,您必须指定列,这是我目前的问题,因为每个列表要检索的列各不相同。

标签: c# arrays multidimensional-array dynamic-arrays


【解决方案1】:
// Init jagged array
String[][] list = new String[6][];

// Creating dynamic column lengths.
list[0] = new String[7];
list[1] = new String[3];
// etc...

// Acessing cell values.
list[0][4] = "test";
// etc...

等等

【讨论】:

  • 或者只是string[][] list = new string[6][] {new string[7], ...};耶 LINQ
猜你喜欢
  • 2021-03-12
  • 1970-01-01
  • 2015-09-24
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
相关资源
最近更新 更多