【问题标题】:How to convert List<string[]> to string[,]?如何将 List<string[]> 转换为 string[,]?
【发布时间】:2014-03-17 16:36:43
【问题描述】:

我成功地将一个二维数组 (string[,]) 转换为下面的列表。现在,如何将下面的列表转换回二维数组 (string[,])?

List<string[]> NameList = new List<string[]>();

这就是我将二维数组转换为列表的方式:

List<string[]> NameList = new List<string[]>();
string[,] Name2DArray = new string[Rows.Count, 3];

for (int i = 0; i < Name2DArray.GetLength(0); i++)
{
   string[] temp = new string[Name2DArray.GetLength(1)];
   for (int n = 0; n < temp.Length; n++)
   {
       temp[n] = Name2DArray[i, n];
   }

   NameList.Add(temp);
 }

【问题讨论】:

  • 到目前为止你尝试过什么?在尝试解决这个问题的过程中,您遇到了什么问题?
  • 如果从数组到列表的转换成功,为什么反过来会出现问题?差别不大。
  • 向我们展示你是如何将其转换为List&lt;string[]&gt;
  • 我添加了将二维数组转换为 List string[] 的代码。
  • @user2142250 太好了。现在向我们展示您迄今为止为解决这个问题所做的尝试,并说明您在尝试解决方案时遇到了什么问题。

标签: c# arrays list


【解决方案1】:

感谢大家的建议。我设法解决了我的代码中的问题。 List string[] 到 2D 数组的转换形式现在正在工作。下面是代码:

string[,] newArray = new string[newTotalRows, 3];
for (int i = 0; i < NameList.Count; i++) 
{
    for (int j = 0; j < NameList[i].Length; j++)
    {        
        newArray[i, j] = NameList[i][j];
    }
}

【讨论】:

    猜你喜欢
    • 2013-08-26
    • 2017-06-03
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多