C#中的List .InsertRange()方法以及示例
C#List<T>.InsertRange()方法
C#List<T>.InsertRange()方法用于在列表中的指定索引处插入相同类型的元素的集合。
语法:
void List<T>.InsertRange(int index, IEnumerable<T> collection);
参数:它接受两个参数:1)index–插入元素的位置;2)collection–T类型元素的集合。
返回值:不返回任何内容–返回类型为void。
示例
int list declaration:
List<int> a = new List<int>();
adding elements:
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
//在指定索引处插入元素(数组)
int[] arr = { 100, 200, 300 };
a.InsertRange(3, arr);
Output:
10 20 30 100 200 300 40 50C#示例使用List<T>.InsertRange()方法在列表中的指定索引处插入元素集合
using System;
using System.Text;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void printList(List<int> lst)
{
//打印元素
foreach (int item in lst)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
//整数列表
List<int> a = new List<int>();
//添加元素
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
//打印列表
Console.WriteLine("列表元素...");
printList(a);
//在指定索引处插入元素(数组)
int[] arr = { 100, 200, 300 };
a.InsertRange(3, arr);
//插入元素后列出
Console.WriteLine("插入元素后列出元素...");
printList(a);
//按ENTER退出
Console.ReadLine();
}
}
}输出结果
列表元素... 10 20 30 40 50 插入元素后列出元素... 10 20 30 100 200 300 40 50
参考:List<T>.InsertRange(Int32,IEnumerable<T>)方法
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志