在C#中的指定索引处将元素插入到集合Collection中
要将元素插入到指定索引处的Collection中,代码如下-
示例
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main(){
Collection<string> col = new Collection<string>();
col.Add("Laptop");
col.Add("Desktop");
col.Add("Notebook");
col.Add("Ultrabook");
col.Add("Tablet");
col.Add("Headphone");
col.Add("Speaker");
Console.WriteLine("Elements in Collection...");
foreach(string str in col){
Console.WriteLine(str);
}
Console.WriteLine("Element at index 3 = " + col[3]);
Console.WriteLine("Element at index 4 = " + col[4]);
col.Insert(5, "Alienware");
Console.WriteLine("Elements in Collection...UPDATED");
foreach(string str in col){
Console.WriteLine(str);
}
}
}输出结果
这将产生以下输出-
Elements in Collection... Laptop Desktop Notebook Ultrabook Tablet Headphone Speaker Element at index 3 = Ultrabook Element at index 4 = Tablet Elements in Collection...UPDATED Laptop Desktop Notebook Ultrabook Tablet Alienware Headphone Speaker
示例
现在让我们来看另一个示例-
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main(){
Collection<string> col = new Collection<string>();
col.Add("Andy");
col.Add("Kevin");
col.Add("John");
col.Add("Kevin");
col.Add("Mary");
col.Add("Katie");
col.Add("Barry");
col.Add("Nathan");
Console.WriteLine("Elements in Collection...");
foreach(string str in col){
Console.WriteLine(str);
}
col.Insert(3, "Jacob");
Console.WriteLine("Elements in Collection...UPDATED");
foreach(string str in col){
Console.WriteLine(str);
}
}
}输出结果
这将产生以下输出-
Elements in Collection... Andy Kevin John Kevin Mary Katie Barry Nathan Elements in Collection...UPDATED Andy Kevin John Jacob Kevin Mary Katie Barry Nathan
热门推荐
10 哥哥中考加油祝福语简短
11 舅舅大婚文案祝福语简短
12 敏字的祝福语简短
13 给老板祝福语简短精辟
14 年底拜年祝福语大全简短
15 项目建设春节祝福语简短
16 中秋祝福语给父亲简短
17 对美好前程祝福语简短
18 给新年祝福语简短独特