C#中遍历ArrayList的三种方法

using System;

using System.Collections;

using System.Linq;

using System.Text;

 

namespace ArrayListDemo

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList();

            arr.Add("How");

            arr.Add("are");

            arr.Add("you");

            arr.Add(100);

            arr.Add(200);

            arr.Add(300);

            arr.Add(1.2);

            arr.Add(22.8);

            //第一种遍历ArrayList的方法

            Console.WriteLine("第一种遍历ArrayList的方法:");

            for (int i = 0; i < arr.Count; i++)

            {

                Console.Write(arr[i].ToString()+" ");

            }

           // Console.Read();

            //第二种遍历ArrayList的方法:

            Console.WriteLine("/n第二种遍历ArrayList的方法:");

            foreach (object o in arr)

            {

                Console.Write(o.ToString() + " ");

            }

            //Console.Read();

 

            //第 三种遍历 ArrayList 对象的方法

            Console.WriteLine("/n第三种遍历ArrayList的方法:");

             IEnumerator ie=arr.GetEnumerator();

            while(ie.MoveNext())

            {

               Console.Write(ie.Current.ToString()+" ");

            }

            Console.Read();

        }

    }

}
发布了177 篇原创文章 · 获赞 31 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_36963950/article/details/103465849