【问题标题】:Add an element/item to a multidimentional list将元素/项目添加到多维列表
【发布时间】:2016-01-08 04:35:25
【问题描述】:

我正在尝试创建一个包含员工及其信息的多维列表。

例如:“简·史密斯”“经理”“75,000”“达拉斯”

我现在拥有的代码给了我一个超出范围的异常。 这个bigROW[i].Add(ownName);bigROW[i][j+1] = newElement; 给了我错误。

 //Begin making rows
        for (int i = 0; i < fileRowCount; i++ )
        { 
            string findOwners = "";
            findOwners = file5Data.Rows[i][0].ToString();
            if(DISTINCTOppOwners.Contains(findOwners))
            {

                //Find index of where owner is 
                    int useIndex = 0;
                    useIndex = DISTINCTOppOwners.IndexOf(findOwners); 

                 //Add their name to Multidimensional list
                  string ownName = DISTINCTOppOwners[useIndex].ToString();
                    //This line give me the ERROR
                    bigROW[i].Add(ownName);

                for (int j = 0; j < fileColCount; j++)
                { 
                     Add Employee information to Multidimensional list
                    string newElement = file5Data.Rows[i][j].ToString();
                    if(ownName != newElement)
                      {

                        if(j ==0)
                        {
                           //Avoid adding their names to the list twice
                          bigROW[i][j+1] = newElement;
                        }

                        bigROW[i][j] = newElement;
                      }
                    }

                }   
        }

我尝试将信息添加到名为“子列表”的列表中,然后将其添加到 BigRow(多维列表),但是当我清除子列表以添加新行时,它从 BigRow 中删除了值。

【问题讨论】:

  • 您可以尝试为员工创建课程并使用List&lt;Employee&gt; 吗?比使用多维数组的东西要好
  • 什么是bigROWList&lt;T&gt;,还是一个数组?它在哪里声明/实例化?

标签: c# list multidimensional-array


【解决方案1】:

我尝试将信息添加到名为“子列表”的列表中,然后将其添加到 BigRow(多维列表),但是当我清除子列表以添加新行时,它从 BigRow 中删除了值。

当您将对象添加到列表时,存储的是引用,而不是对象的内容。而不是清除sublist,您应该每次都创建一个新的List。否则,您有一个外部列表,其中包含同一列表的多个副本。

请参阅上面的jdweng's answer 以获取此示例。在他的代码中,ToList 调用为每一行创建了一个新的numbers 列表,因此每一行都有自己的List 数字。

【讨论】:

    【解决方案2】:

    这是一个简单的例子

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleApplication64
    {
        class Program
        {
            static void Main(string[] args)
            {
                string input =
                    "0,1,2,3,4,5,6,7,8,9\n" +
                    "10,11,12,13,14,15,16,17,18,19\n" +
                    "20,21,22,23,24,25,26,27,28,29\n" +
                    "30,31,32,33,34,35,36,37,38,39\n" +
                    "40,41,42,43,44,45,46,47,48,49\n";
    
                List<List<int>> output = new List<List<int>>();
                StringReader reader = new StringReader(input);
                string inputline = "";
    
                while ((inputline = reader.ReadLine()) != null)
                {
                    List<int> numbers = inputline.Split(new char[] { ',' }).Select(x => int.Parse(x)).ToList();
                    output.Add(numbers);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-25
      • 2021-10-06
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 2021-05-30
      • 2014-04-18
      相关资源
      最近更新 更多