集合和迭代器,迭代器模式

迭代器

就是用來遍歷的

引例:自己做一個List叫MyList,實現一個迭代器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text5
{
    class Program
    {
        class MyList
        {
            private int[] Nums { get; set; }

            public MyList(int n)
            {
                var r = new Random();
                Nums = new int[n];
                for (var i = 0; i < n; i++)
                {
                    Nums[i] = r.Next(0, 10);
                }
            }
            private int Index=-1;
            public bool MoveNext()
            {
                Index++;
                return  Index < Nums.Length;
            }
            public int Current { get { return Nums[Index]; } }

        }
        static void Main(string[] args)
        {
            MyList List = new MyList(5);
            while (List.MoveNext())
            {
                Console.WriteLine(List.Current);
            }
            Console.ReadLine();
        }
    }
}
image.png

現在要求打印兩次元素,加一個要求例如生成的三個數為1,2,3
想要輸出:
1
——1
——2
——3
2
——1
——2
——3
3
——1
——2
——3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text5
{
    class Program
    {
        class Enumerator//定義一個迭代器
        {
            private int[] Nums=null;
            public Enumerator(int[] nums)
            {
                this.Nums = nums;
            }
            private int Index = -1;
            public bool MoveNext()
            {
                Index++;
                return Index < Nums.Length;
            }
            public int Current { get { return Nums[Index]; } }
        }
        class MyList
        {
            private int[] Nums { get; set; }

            public MyList(int n)
            {
                var r = new Random();
                Nums = new int[n];
                for (var i = 0; i < n; i++)
                {
                    Nums[i] = r.Next(0, 10);
                }
            }
            public Enumerator GetEnumerator()
            {
                return new Enumerator(Nums) ; 
            }
            //private int Index=-1;
            //public bool MoveNext()
            //{
            //    Index++;
            //    return  Index < Nums.Length;
            //}
            //public int Current { get { return Nums[Index]; } }

        }
        static void Main(string[] args)
        {
            MyList List = new MyList(5);
            Enumerator e1 = List.GetEnumerator();
            
            while (e1.MoveNext())
            {
                Console.WriteLine(e1.Current);
                Enumerator e2 = List.GetEnumerator();
                while (e2.MoveNext())
                {
                    Console.WriteLine("---" + e2.Current);
                }
            }
            Console.ReadLine();
        }
    }
}

image.png

C#已經將迭代器模式已經進行進一步封裝

集合

集合就是實現了IEnumerable的接口
下面我們來實現IEnumerable接口的自定義的集合

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text5
{
    class Program
    {
        //class Enumerator//定義一個迭代器
        //{
        //    private int[] Nums=null;
        //    public Enumerator(int[] nums)
        //    {
        //        this.Nums = nums;
        //    }
        //    private int Index = -1;
        //    public bool MoveNext()
        //    {
        //        Index++;
        //        return Index < Nums.Length;
        //    }
        //    public int Current { get { return Nums[Index]; } }
        //}
        class MyList:IEnumerable <int>
        {
            private int[] Nums { get; set; }

            public MyList(int n)
            {
                var r = new Random();
                Nums = new int[n];
                for (var i = 0; i < n; i++)
                {
                    Nums[i] = r.Next(0, 10);
                }
            }

            public IEnumerator<int> GetEnumerator()
            {
                foreach (var item in Nums )
                {
                    yield return item;
                }
                //throw new NotImplementedException();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
                //throw new NotImplementedException();
            }
            //public Enumerator GetEnumerator()
            //{
            //    return new Enumerator(Nums) ; 
            //}

            //private int Index=-1;
            //public bool MoveNext()
            //{
            //    Index++;
            //    return  Index < Nums.Length;
            //}
            //public int Current { get { return Nums[Index]; } }

        }
        static void Main(string[] args)
        {
            MyList List = new MyList(5);
            foreach (var i1 in List)
            {
                Console.WriteLine(i1);
                foreach (var i2 in List)
                {
                    Console.WriteLine("---" + i2);
                }
            }
            //var e1 = List.GetEnumerator();
            //while (e1.MoveNext())
            //{
            //    Console.WriteLine(e1.Current);
            //    var e2 = List.GetEnumerator();
            //    while (e2.MoveNext())
            //    {
            //        Console.WriteLine("---" + e2.Current);
            //    }
            //}
            Console.ReadLine();
        }
    }
}

image.png

foreach 實現里的接口

常用集合

List<>
Dictionary<>
Strack<>
Queue<>
LinkedList<>

  static void Main(string[] args)
        {
             var dic = new Dictionary<int, string>();
            dic.Add(1, "張三");
            dic.Add(2, "李四");
            dic.Add(3, "張三");
            dic.Add(4, "張三");
            foreach (var item in dic)
            {
                Console.WriteLine( item.Key.ToString());
                Console.WriteLine( item.Value);
            }
             Console.ReadLine();
        }
image.png
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 在一個方法內部定義的變量都存儲在棧中,當這個函數運行結束后,其對應的棧就會被回收,此時,在其方法體中定義的變量將不...
    Y了個J閱讀 4,438評論 1 14
  • 一、數組數組是一組使用數字索引的對象,這些對象屬于同一種類型。雖然C#為創建數組提供了直接的語言支持,但通用類型系...
    CarlDonitz閱讀 685評論 0 1
  • 集合是.NET FCL(Framework Class Library)的重要組成部分,我們平常擼C#代碼時免不了...
    aslbutton閱讀 962評論 0 50
  • 集合類框架的介紹: ![Java 集合類框架](https://upload-images.jianshu.io/...
    LynnGuo閱讀 763評論 0 1
  • Java集合類可用于存儲數量不等的對象,并可以實現常用的數據結構如棧,隊列等,Java集合還可以用于保存具有映射關...
    小徐andorid閱讀 1,965評論 0 13