在 C# 中向 ArrayList 末尾添加元素
若要向 ArrayList 末尾添加元素,可使用以下代码:
示例
using System; using System.Collections; public class Demo { public static void Main(String[] args) { ArrayList list = new ArrayList(); list.Add("Andy"); list.Add("Gary"); list.Add("Katie"); list.Add("Amy"); Console.WriteLine("Elements in ArrayList..."); foreach (string res in list) { Console.WriteLine(res); } string[] strArr = { "John", "Jacob" }; list.AddRange(strArr); Console.WriteLine("Elements in ArrayList...UPDATED"); foreach(String str in list) { Console.WriteLine(str); } } }
输出
这将生成以下输出:
Elements in ArrayList... Andy Gary Katie Amy Elements in ArrayList...UPDATED Andy Gary Katie Amy John Jacob
示例
我们再来看另一个示例:
using System; using System.Collections; public class Demo { public static void Main(String[] args) { ArrayList list = new ArrayList(); list.Add("Andy"); list.Add("Gary"); list.Add("Katie"); list.Add("Amy"); Console.WriteLine("Elements in ArrayList..."); foreach (string res in list) { Console.WriteLine(res); } string[] strArr = { "John", "Jacob" }; list.AddRange(strArr); Console.WriteLine("Elements in ArrayList...UPDATED"); foreach(String str in list) { Console.WriteLine(str); } string[] strArr2 = { "Tim", "Tom", "David" }; list.AddRange(strArr2); Console.WriteLine("Elements in ArrayList...UPDATED"); foreach(String str in list) { Console.WriteLine(str); } } }
输出
这将生成以下输出:
Elements in ArrayList... Andy Gary Katie Amy Elements in ArrayList...UPDATED Andy Gary Katie Amy John Jacob Elements in ArrayList...UPDATED Andy Gary Katie Amy John Jacob Tim Tom David
广告