Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串添加到二维列表的第一个维度_C#_.net_List - Fatal编程技术网

C# 将字符串添加到二维列表的第一个维度

C# 将字符串添加到二维列表的第一个维度,c#,.net,list,C#,.net,List,我已经创建了一个二维列表。我想使用for循环为两个维度添加两个不同的字符串。但问题是,有任何选项作为Add用于在第一维度中保存我的字符串。有人能帮忙吗 List<Tuple<int, List<string>>> List_type1 = new List<Tuple<int, List<string>>>(); List<Tuple<int, List<string>

我已经创建了一个二维
列表
。我想使用
for
循环为两个维度添加两个不同的字符串。但问题是,有任何选项作为
Add
用于在第一维度中保存我的字符串。有人能帮忙吗

        List<Tuple<int, List<string>>> List_type1 = new List<Tuple<int, List<string>>>();
        List<Tuple<int, List<string>>> List_type2 = new List<Tuple<int, List<string>>>();            
        int indx1;
        int indx2;
        List<TreeNode> IndexList1 = new List<TreeNode>();
        IndexList1 = FindIndex(treeView1.Nodes, IndexList1);
        indx1 = IndexList1[0].Index;

        foreach (TreeNode node1 in nodes1)
        {
            for (int i = 0; i < actions1.Count; i++)
            {
                List_type1.Add(new Tuple<int, List<string>>(i, new List<string>()));
                TreeNode str1 = node1.Nodes[indx1].Nodes[i];
                string TypeAction1 = actions1[i].Attributes["type"].Value;
                string NameAction1 = actions1[i].Attributes["name"].Value;
                List_type1[i].Item1.Equals(TypeAction1);
                List_type1[i].Item2.Add(NameAction1);
            }
        }
List List_type1=新列表();
列表类型2=新列表();
int-indx1;
int-indx2;
List IndexList1=新列表();
IndexList1=FindIndex(treeView1.Nodes,IndexList1);
indx1=IndexList1[0]。索引;
foreach(节点1中的树节点1)
{
for(int i=0;i

其中
nodes1
TreeNodeCollection
FindIndex
查找
TreeNodes
中的节点数

当你这么做的时候

List_type1[i].Item1.Equals(TypeAction1);
列表为空。您应该添加新的元组,然后进行比较。但是你的比较是没有用的?因为不使用它的结果

另一种方法是在循环之前初始化列表

或者,您可能混淆了两行代码,您的意思是:

List_type1[i].Item2.Add(NameAction1);
List_type1[i].Item1.Equals(TypeAction1);

但无论如何,第二行代码没有任何意义。

当您执行此操作时

List_type1[i].Item1.Equals(TypeAction1);
列表为空。您应该添加新的元组,然后进行比较。但是你的比较是没有用的?因为不使用它的结果

另一种方法是在循环之前初始化列表

或者,您可能混淆了两行代码,您的意思是:

List_type1[i].Item2.Add(NameAction1);
List_type1[i].Item1.Equals(TypeAction1);
但无论如何,第二行代码没有任何意义。

行中:

List_type1[i].Item1.Equals(TypeAction1);
通过索引调用对象,但列表为空。

行中:

List_type1[i].Item1.Equals(TypeAction1);

通过索引调用对象,但列表为空。

我发现它可能如下所示:

        List<Tuple<int, List<string>, List<string>>> List_type1 = new List<Tuple<int, List<string>, List<string>>>();
        foreach (TreeNode node1 in nodes1)
        {
            for (int i = 0; i < actions1.Count; i++)
            {
                List_type1.Add(new Tuple<int, List<string>, List<string>>(i, new List<string>(), new List<string>()));
                TreeNode str1 = node1.Nodes[indx1].Nodes[i];
                list1.Add(str1);
                string TypeAction1 = actions1[i].Attributes["type"].Value;
                string NameAction1 = actions1[i].Attributes["name"].Value;
                List_type1[i].Item2.Add(TypeAction1);
                List_type1[i].Item3.Add(NameAction1);
            }
        }

我发现可能是这样的:

        List<Tuple<int, List<string>, List<string>>> List_type1 = new List<Tuple<int, List<string>, List<string>>>();
        foreach (TreeNode node1 in nodes1)
        {
            for (int i = 0; i < actions1.Count; i++)
            {
                List_type1.Add(new Tuple<int, List<string>, List<string>>(i, new List<string>(), new List<string>()));
                TreeNode str1 = node1.Nodes[indx1].Nodes[i];
                list1.Add(str1);
                string TypeAction1 = actions1[i].Attributes["type"].Value;
                string NameAction1 = actions1[i].Attributes["name"].Value;
                List_type1[i].Item2.Add(TypeAction1);
                List_type1[i].Item3.Add(NameAction1);
            }
        }

错误发生在哪一行?它位于以下行:
List_type1[i].Item1.Equals(TypeAction1)以及
FindIndex
的作用是什么?这是您的自定义方法吗?错误发生在哪一行?它位于以下行:
List_type1[i].Item1.Equals(TypeAction1)以及
FindIndex
的作用是什么?这是您的自定义方法吗?在这个答案中,您甚至不需要有
列表
,因为它总是只有一个元素。。。此外,您将拥有
actions.Count*nodes1.Count
列表中的项目数…在这个答案中,您甚至不需要拥有
list
,因为它总是只有一个元素。。。此外,您将拥有
actions.Count*nodes1.Count
列表中的项目数。。。