首页 > 代码库 > 自己定义泛型集合并实现foreach遍历

自己定义泛型集合并实现foreach遍历

1.首先创建一下SelfList<T>类;

  public class SelfList<T>:IEnumerable

  {
 

  }

2.添加默认的初始大小为4的Item数组;

  

public class SelfList<T>:IEnumerable

{
 /// <summary>
  /// 默认数组
  /// </summary>
  private T[] items=new T[4];
  public T[] Items
  {
    get { return items; }
    set { items = value; }
  }

}

3.添加只读的Count属性,用于记录Item数组中元素的个数

public class SelfList<T>:IEnumerable

{
  /// <summary>
  /// 默认数组
  /// </summary>
  private T[] items=new T[4];
  public T[] Items
  {
    get { return items; }
    set { items = value; }
  }

  /// <summary>
  /// 记录数组中元素的个数
  /// </summary>
  private int count;
  public int Count
  {
    get { return count; }
  }

}

4.添加向数组中增加元素的方法

/// <summary>
/// 向数组中添加元素
/// </summary>
/// <param name="item"></param>
public void Add(T item)
{
  if (count==items.Length)
  {
    T[] newArr=new T[items.Length*2];
    items.CopyTo(newArr,0);
    items = newArr;
  }
  items[count] = item;
  count++;
}

5.添加删除元素的方法

  public void RemoveAt(int index)
  {
    if (index < 0 || index >= items.Length)
    {
      throw new Exception("下标越界");
    }
    Array.Copy(items,index+1,items,index,items.Length-index-1);
    count--;
  }

6.添加索引

  public T this[int index]
  {
    get
    {
      if (index < 0 || index>=items.Length)
    {
      throw new Exception("下标越界");
    }
    return items[index];
  }
  set
  {
    if (index < 0 || index >= items.Length)
    {
      throw new Exception("下标越界");
    }
    items[index] = value;
    }
  }

7.要实现对集合的遍历需要实现 IEnumerable接口并重写 GetEnumerator()方法,方法有一个IEnumerator接口的返回值,所以我们要自己定义一个实现了IEnumerator

  接口的类

public class SelfList<T>:IEnumerable

{

  public IEnumerator GetEnumerator()
  {
    return new MyEnumerator<T>(items,count);
  }

}

public class MyEnumerator<T> : IEnumerator
{

  private T[] temp;//记录要遍历的数组
  private int num;//记录数组中元素的个数
  private int index = -1;
  public MyEnumerator(T[] temp,int num)
  {
    this.temp = temp;
    this.num = num;
  }

  /// <summary>
  /// 得到现在下标指向的元素
  /// </summary>

  public object Current
  {
    get { return temp[index]; }
  } 

  /// <summary>
  /// 判断下一个位置是否还有元素
  /// </summary>
  /// <returns></returns>

  public bool MoveNext()
  {
    index++;
    if (index<num)
    {
      return true;
    }
    return false;
  }  

  /// <summary>
  /// 初始化遍历位置
  /// </summary>

  public void Reset()
  {
    index = -1;
  }

}

8.最后在Main方法中就可以实现集合的遍历:

static void Main(string[] args)
{  

  SelfList<int> lists=new SelfList<int>();

  lists.Add(1);
  lists.Add(2);
  lists.Add(3);

  foreach(int item in lists)

  {

    Console.WriteLine(item+"");

  }

}

自定义集合的全部实现代码:

public class SelfList<T>:IEnumerable
{
  /// <summary>
  /// 默认数组
  /// </summary>
  private T[] items=new T[4];
  public T[] Items
  {
    get { return items; }
    set { items = value; }
  }
  /// <summary>
  /// 记录数组中元素的个数
  /// </summary>
  private int count;
  public int Count
  {
    get { return count; }
  }
  /// <summary>
  /// 向数组中添加元素
  /// </summary>
  /// <param name="item"></param>
  public void Add(T item)
  {
    f (count==items.Length)
    {
      T[] newArr=new T[items.Length*2];
      items.CopyTo(newArr,0);
      items = newArr;
    }
    items[count] = item;
    count++;
  }

  /// <summary>
  /// 数组下标索引
  /// </summary>
  /// <param name="index"></param>
  /// <returns></returns>
  public T this[int index]
  {
    get
    {
      if (index < 0 || index>=items.Length)
    {
      throw new Exception("下标越界");
    }
    return items[index];
  }
  set
  {
    if (index < 0 || index >= items.Length)
    {
      throw new Exception("下标越界");
    }
    items[index] = value;
    }
  }
  /// <summary>
  ///
  /// </summary>
  /// <param name="index"></param>
  public void RemoveAt(int index)
  {
    if (index < 0 || index >= items.Length)
    {
      throw new Exception("下标越界");
    }
    Array.Copy(items,index+1,items,index,items.Length-index-1);
    count--;
  }

  public IEnumerator GetEnumerator()
  {
    return new MyEnumerator<T>(items,count);
  }
}