要将对象添加到ArrayList的末尾,代码如下-
using System; using System.Collections; public class Demo { public static void Main(String[] args){ ArrayList list = new ArrayList(); list.Add("Tim"); list.Add("Katie"); list.Add("Amy"); list.Add("Carlos"); list.Add("Chris"); list.Add("Jason"); Console.WriteLine("Elements in ArrayList..."); foreach (string res in list){ Console.WriteLine(res); } } }
输出结果
这将产生以下输出-
Elements in ArrayList... Tim Katie Amy Carlos Chris Jason
现在让我们来看另一个示例-
using System; using System.Collections; public class Demo { public static void Main(String[] args){ ArrayList list1 = new ArrayList(); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D"); list1.Add("E"); list1.Add("F"); list1.Add("G"); list1.Add("H"); list1.Add("I"); Console.WriteLine("Elements in ArrayList1..."); foreach (string res in list1){ Console.WriteLine(res); } ArrayList list2 = new ArrayList(); list2.Add("1"); list2.Add("2"); list2.Add("3"); list2.Add("4"); list2.Add("5"); list2.Add("6"); Console.WriteLine("Elements in ArrayList2..."); foreach (string res in list2){ Console.WriteLine(res); } Console.WriteLine("Is ArrayList1 equal to ArrayList2? = "+list1.Equals(list2)); } }
输出结果
这将产生以下输出-
Elements in ArrayList1... A B C D E F G H I Elements in ArrayList2... 1 2 3 4 5 6 Is ArrayList1 equal to ArrayList2? = False