zoukankan      html  css  js  c++  java
  • 简单迭代器

    抽象类Iterator

    namespace myIterator
    {
        public abstract class Iterator
        {
            public abstract bool HasNext();
            public abstract object Current();
        }
    }
    View Code

    抽象集合类

    namespace myIterator
    {
        public abstract class Aggregate
        {
            public abstract Iterator Iterator();
        }
    }
    View Code

    Iterator实现类

    namespace myIterator
    {
        public class ConcreteIterator : Iterator
        {
            private readonly ConcreteAggregate _aggregate;
            private int _current;
    
            public ConcreteIterator(ConcreteAggregate aggregate)
            {
                this._aggregate = aggregate;
            }
    
            public override bool HasNext()
            {
                return _current < _aggregate.Count ? true : false;
            }
    
            public override object Current()
            {
                return _aggregate[_current++];
            }
        }
    }
    View Code

    集合实现类

    using System;
    using System.Runtime;
    
    namespace myIterator
    {
        public class ConcreteAggregate : Aggregate
        {
            private readonly object[] _emptyArray = new object[0];
            private object[] _items = new object[4];
            private int now;
    
    
            public int Count
            {
                get { return _items.Length; }
            }
    
            public object this[int index]
            {
                get { return _items[index]; }
                set
                {
                    if (now < _items.Length)
                        _items[now++] = value;
                    else
                    {
                        var size = (int) (_items.Length*1.5);
                        var tem = new object[size];
                        Array.Copy(_items, 0, tem, 0, _items.Length);
                        _items = tem;
                        tem = null;
                    }
                }
            }
    
            public int Capacity
            {
                [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
                get
                {
                    return _items.Length;
                }
                set
                {
                    if (value < now)
                        throw new Exception();
                    if (value == _items.Length)
                        return;
                    if (value > 0)
                    {
                        var objArray = new object[value];
                        if (now > 0)
                            Array.Copy(_items, 0, objArray, 0, now);
                        _items = objArray;
                    }
                    else
                        _items = _emptyArray;
                }
            }
    
            public override Iterator Iterator()
            {
                return new ConcreteIterator(this);
            }
    
            public void Add(object item)
            {
                if (now == _items.Length)
                    EnsureCapacity(now + 1);
                _items[now++] = item;
            }
    
            private void EnsureCapacity(int min)
            {
                if (_items.Length >= min)
                    return;
                int num = _items.Length == 0 ? 4 : _items.Length*2;
                if (num < min)
                    num = min;
                Capacity = num;
            }
        }
    }
    View Code

    测试类

    using System;
    
    namespace myIterator
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                T0();
                Console.ReadKey();
            }
    
    
            private static void T0()
            {
                var ca = new ConcreteAggregate();
                ca.Add("a");
                ca.Add("b");
                ca.Add("c");
                ca.Add("d");
                ca.Add("e");
    
                Iterator itor = ca.Iterator();
                while (itor.HasNext())
                {
                    Console.WriteLine(itor.Current());
                }
            }
        }
    }
    View Code
  • 相关阅读:
    rem适配布局---5. 方案1:苏宁首页制作1
    rem适配布局---4. rem适配方案
    rem适配布局---3. less
    rem适配布局---2. 媒体查询
    rem适配布局---1. 基础
    flex布局---9.携程网案例
    java基础---3. 数据类型转换、运算符
    flex布局---8.flex布局原理
    java基础---2. 常量&变量
    工会项目结题,游泳锻炼
  • 原文地址:https://www.cnblogs.com/lansor/p/3303103.html
Copyright © 2011-2022 走看看