using System; using System.Collections; class TestArrayList { staticvoid Main() { ArrayList theArrayList = new ArrayList(); theArrayList.Add("1"); theArrayList.Add("2"); foreach(string s in theArrayList) { Console.WriteLine(s); } } }
using System; using System.Collections; class TestArrayList { staticvoid Main() { ArrayList theArrayList = new ArrayList(); theArrayList.Add("1"); theArrayList.Add("2"); foreach(string s in theArrayList) { Console.WriteLine(s); } theArrayList.Clear(); Console.WriteLine(theArrayList.Count); Console.Read(); } }
using System; using System.Collections; class TestArrayList { staticvoid Main() { ArrayList theArrayList = new ArrayList(); theArrayList.Add("1"); theArrayList.Add("2"); foreach(string s in theArrayList) { Console.WriteLine(s); } if (theArrayList.Contains("1")) //判断字符中"1"是否存在于ArrayList中 { Console.WriteLine("Yes"); } else { Console.WriteLine("NO"); } Console.Read(); } }
CopyTo() 将ArrayList 全部内容拷贝到一个一维数组中
using System; using System.Collections; class TestArrayList { staticvoid Main() { ArrayList theArrayList = new ArrayList(); theArrayList.Add("1"); theArrayList.Add("2"); string [] a =newstring[2]; theArrayList.CopyTo(a); for (int i =0; i < a.Length; i++) { Console.WriteLine(a[i]); } Console.Read(); } }
IndexOf()
using System; using System.Collections; class TestArrayList { staticvoid Main() { ArrayList theArrayList = new ArrayList(); string str1 ="1"; string str2 ="2"; int num1 =1; int num2 =2; theArrayList.Add(str1); theArrayList.Add(str2); theArrayList.Add(num1); theArrayList.Add(num2); Console.WriteLine(theArrayList.IndexOf(num1)); //返回改对象在ArrayList中的索引值 Console.WriteLine(theArrayList.IndexOf(str1)); Console.Read(); } }