📌  相关文章
📜  C#|如何将集合的元素插入到指定索引处的List中

📅  最后修改于: 2021-05-29 21:58:18             🧑  作者: Mango

List .InsertRange(Int32,IEnumerable )方法用于将集合的元素插入到指定索引处的List 中。

列表的属性:

  • 它与数组不同。列表可以动态调整大小,但数组则不能。
  • 列表类可以接受null作为引用类型的有效值,并且还允许重复的元素。
  • 如果计数等于容量,则列表的容量将通过重新分配内部数组而自动增加。在添加新元素之前,现有元素将被复制到新数组。

句法:

public void InsertRange (int index, System.Collections.Generic.IEnumerable collection);

范围:

注意:集合本身不能为null。但是,如果类型T是引用类型,则它可以包含可以为null的元素。

例外情况:

  • ArgumentNullException:如果集合为null。
  • ArgumentOutOfRangeException:如果索引小于零或大于count。

下面的程序说明了上面讨论的方法的使用:

范例1:

// C# Program to insert the elements of
// a collection into the List at the
// specified index
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        string[] str1 = { "Geeks",
                          "for",
                          "Geeks" };
  
        // Creating an List of strings
        // adding str1 elements to List
        List firstlist = new List(str1);
  
        // displaying the elements of firstlist
        Console.WriteLine("Elements in List: \n");
  
        foreach(string dis in firstlist)
        {
            Console.WriteLine(dis);
        }
  
        Console.WriteLine(" ");
  
        // contains new Elements which is
        // to be added in the List
        str1 = new string[] { "New",
                              "Element",
                              "Added" };
  
        // using InsertRange Method
        Console.WriteLine("InsertRange(2, str1)\n");
  
        // adding elements after 2nd
        // index of the List
        firstlist.InsertRange(2, str1);
  
        // displaying the elements of
        // List after InsertRange Method
        foreach(string res in firstlist)
        {
            Console.WriteLine(res);
        }
    }
}

输出:

Elements in List: 

Geeks
for
Geeks
 
InsertRange(2, str1)

Geeks
for
New
Element
Added
Geeks

范例2:

// C# Program to insert the elements of
// a collection into the List at the
// specified index
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        string[] str1 = { "Geeks",
                          "for",
                          "Geeks" };
  
        // Creating an List of strings
        // adding str1 elements to List
        List firstlist = new List(str1);
  
        // displaying the elements of firstlist
        Console.WriteLine("Elements in List: \n");
  
        foreach(string dis in firstlist)
        {
            Console.WriteLine(dis);
        }
  
        Console.WriteLine(" ");
  
        // contains new Elements which is
        // to be added in the List
        str1 = new string[] { "New",
                              "Element",
                              "Added" };
  
        // using InsertRange Method
        Console.WriteLine("InsertRange(2, str1)\n");
  
        // this will give error as
        // index is less than 0
        firstlist.InsertRange(-1, str1);
  
        // displaying the elements of
        // List after InsertRange Method
        foreach(string res in firstlist)
        {
            Console.WriteLine(res);
        }
    }
}

错误:

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1.insertrange?view=netframework-4.7.2