C#的foreach循环替换for循环
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace Test1111 { class Program { static void Main(string[] args) { ArrayList studentList = new ArrayList(); studentList.Add("a"); studentList.Add("b"); studentList.Add("c"); studentList.Add("d"); studentList.Add("e"); studentList.AddRange(new int[]{6,7,8,9}); studentList.Add("ssss"); Console.WriteLine("集合元素为:"); //for (int i = 0; i<studentList.Count;i++ ) //{ // Console.Write(studentList[i]); //} foreach (object x in studentList) { Console.Write("{0} ", x); } Console.WriteLine(); studentList.Remove("e"); studentList.RemoveAt(1); studentList.RemoveRange(4,2); Console.WriteLine("集合元素为:"); //for (int i = 0; i < studentList.Count; i++) //{ // Console.Write(studentList[i]); //} foreach (object x in studentList) { Console.Write("{0} ", x); } Console.WriteLine(); studentList.Insert(1,"fff"); studentList.InsertRange(0,new string[]{"张三","李四"}); Console.WriteLine("集合元素为:"); //for (int i = 0; i < studentList.Count; i++) //{ // Console.Write(studentList[i]); //} foreach (object x in studentList) { Console.Write("{0} ", x); } Console.WriteLine(); studentList.Add("王五"); if (!studentList.Contains("王五")) { studentList.Add("王五"); } else { Console.WriteLine("集合中已经有王五了!"); } //for (int i = 0; i<studentList.Count;i++ ) //{ // Console.Write(studentList[i]); //} foreach (object x in studentList) { Console.Write("{0} ", x); } Console.ReadLine(); } } }