【问题标题】:c# Add variable to list in a specific index [closed]c#将变量添加到特定索引中的列表[关闭]
【发布时间】:2014-04-13 12:54:39
【问题描述】:

我想知道如何将变量添加到特定的列表索引。举个例子吧。。

int a = 4;
List<int> listB = new List<int>();

for(int i = 0; i < 10, i++)
{
     if (a == i)
     {
          listB.(something like add to index, this is the part I dont know what to do, my guess would be listB.AddToIndex(a, a) for adding variable a to index a in the list.)
     }
}

希望我的解释可以理解,提前谢谢。

【问题讨论】:

  • 这个问题似乎离题了,因为它没有显示任何研究工作。

标签: c# list for-loop add


【解决方案1】:

您可以使用插入功能:

listB.Insert(a, item);

【讨论】:

    【解决方案2】:

    List&lt;T&gt; 上的.Insert() 方法正是为此目的

    如果你想在第一个索引处插入

      listB.Insert(1, someValue);
    

    【讨论】:

      【解决方案3】:

      你可以用这个

               listB.Insert(a,a)
      

      【讨论】:

        【解决方案4】:

        使用listB.Add(item) 在列表末尾添加一个项目。

        使用listB.Insert(index, item) 选择项目的插入位置。

        【讨论】:

          【解决方案5】:

          Insert(index, item) 方法允许您执行此操作。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-09-05
            • 2021-01-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-01
            • 1970-01-01
            • 2020-10-03
            相关资源
            最近更新 更多