将元素插入C#中指定索引处的ArrayList中
要将元素插入到指定索引处的ArrayList中,代码如下-
示例
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList list = new ArrayList();
list.Add("One");
list.Add("Two");
list.Add("Three");
list.Add("Four");
list.Add("Five");
list.Add("Six");
list.Add("Seven");
list.Add("Eight");
Console.WriteLine("ArrayList elements...");
foreach(string str in list) {
Console.WriteLine(str);
}
Console.WriteLine("ArrayList is read-only? = "+list.IsReadOnly);
Console.WriteLine("Does the element Six in the ArrayList? = "+list.Contains("Six"));
list.Insert(4, "Twelve");
Console.WriteLine("ArrayList elements...UPDATED");
foreach(string str in list) {
Console.WriteLine(str);
}
}
}输出结果
这将产生以下输出-
ArrayList elements... One Two Three Four Five Six Seven Eight ArrayList is read-only? = False Does the element Six in the ArrayList? = True ArrayList elements...UPDATED One Two Three Four Twelve Five Six Seven Eight
示例
让我们看另一个例子-
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add(100);
arrList.Add(200);
arrList.Add(300);
arrList.Add(400);
arrList.Add(500);
Console.WriteLine("Display elements...");
IEnumerator demoEnum = arrList.GetEnumerator();
while (demoEnum.MoveNext()) {
Object ob = demoEnum.Current;
Console.WriteLine(ob);
}
arrList.Insert(4, 1000);
Console.WriteLine("Display elements...UPDATED");
demoEnum = arrList.GetEnumerator();
while (demoEnum.MoveNext()) {
Object ob = demoEnum.Current;
Console.WriteLine(ob);
}
}
}输出结果
这将产生以下输出-
Display elements... 100 200 300 400 500 Display elements...UPDATED 100 200 300 400 1000 500
热门推荐
10 蛋糕生日弥勒祝福语简短
11 拥有爱的祝福语简短
12 周末祝福语正能量简短
13 看病贺卡祝福语简短英文
14 考生大学后祝福语简短
15 女朋友祝福语简短新年
16 中午生日家庭祝福语简短
17 父母生孩子祝福语简短
18 元旦联谊祝福语简短精辟