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

迭代器

就是用来遍历的

引例:自己做一个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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在一个方法内部定义的变量都存储在栈中,当这个函数运行结束后,其对应的栈就会被回收,此时,在其方法体中定义的变量将不...
    Y了个J阅读 4,616评论 1 14
  • 一、数组数组是一组使用数字索引的对象,这些对象属于同一种类型。虽然C#为创建数组提供了直接的语言支持,但通用类型系...
    CarlDonitz阅读 834评论 0 1
  • 集合是.NET FCL(Framework Class Library)的重要组成部分,我们平常撸C#代码时免不了...
    aslbutton阅读 1,035评论 0 50
  • 集合类框架的介绍: ![Java 集合类框架](https://upload-images.jianshu.io/...
    LynnGuo阅读 812评论 0 1
  • Java集合类可用于存储数量不等的对象,并可以实现常用的数据结构如栈,队列等,Java集合还可以用于保存具有映射关...
    小徐andorid阅读 2,114评论 0 13

友情链接更多精彩内容