zoukankan      html  css  js  c++  java
  • IEnumerable IEnumerator 自己定义迭代器

       public class Person
        {
            string Name;
            int Age;
    
            public Person(string name, int age)
            {
                Name = name;
                Age = age;
            }
    
            public override string ToString()
            {
                return "Name: " + Name + "	Age: " + Age;
            }
        }
    
    
      public class People : IEnumerable
        {
            private Person[] _people;
            public People(Person[] pArray)
            {
                _people = new Person[pArray.Length];
                for (int i = 0; i < pArray.Length; i++)
                {
                    _people[i] = pArray[i];
    
                }
            }
    
            public IEnumerator GetEnumerator()
            {
                return new PeopleEnum(_people);
            }
        }
    
      public class PeopleEnum : IEnumerator
        {
            private Person[] _people;
            int position = -1;
    
            public PeopleEnum(Person[] list)
            {
                _people = list;
            }
    
            public bool MoveNext()
            {
                position++;
                return (position < _people.Length);
            }
    
            public void Reset()
            {
                position = -1;
            }
    
            public object Current
            {
                get
                {
                    return _people[position];
                }
            }
    
        }
  • 相关阅读:
    Servlet CDI Example Analysis
    JSON续
    JSON
    Ubuntu 下安装 MySQL
    Filter介绍
    Annotation相关
    Container、Injection
    Java buildTools
    Http协议
    URI URL URN 的区别
  • 原文地址:https://www.cnblogs.com/lemonP/p/7059551.html
Copyright © 2011-2022 走看看