自己写一个泛型集合类型,可实现添加和遍历
2023-09-11 14:16:51 时间
在"C#中List<T>是怎么存放元素的"中,分析了List<T>的源码,了解了List<T>是如何存放元素的。这次,就自定义一个泛型集合类型,可实现添加元素,并支持遍历。
该泛型集合类型一定需要一个添加元素的方法,在添加元素的时候需要考虑:当添加的元素超过当前数组的容量,就让数组扩容;为了支持循环遍历,该泛型集合类型必须提供一个迭代器(实现IEnumerator接口)。
public class MyList<T>{T[] items = new T[5];
private int count;public void Add(T item){if(count == items.Length)
Array.Resize(ref items, items.Length * 2);
items[count++] = item;}public IEnumerator<T> GetEnumerator()
{return new MyEnumeraor(this);}class MyEnumeraor : IEnumerator<T>
{private int index = -1;private MyList<T> _myList;
public MyEnumeraor(MyList<T> myList)
{_myList = myList;}public T Current
{get
{if (index < 0 || index >= _myList.count)
{return default(T);}return _myList.items[index];
}}public void Dispose(){}object System.Collections.IEnumerator.Current
{get { return Current; }}public bool MoveNext(){return ++index < _myList.count;
}public void Reset(){index = -1;}}}
○ 泛型集合类型维护着一个T类型的泛型数组
○ 私有字段count是用来计数的,每添加一个元素计数加1
○ 添加方法考虑了当count计数等于当前元素的长度,就让数组扩容为当前的2倍
○ 迭代器实现了IEnumerator<T>接口
客户端调用。
class Program
{static void Main(string[] args){MyList<int> list = new MyList<int>();list.Add(1);list.Add(2);foreach (int item in list){Console.WriteLine(item);}Console.ReadKey();}}
另外,IEnumerable和IEnumerator的区别是什么呢?
其实,真正执行迭代的是IEnumerator迭代器。IEnumerable接口就提供了一个方法,就是返回IEnumerator迭代器。
public interface IEnumerable{IEnumerator GetEnumerator();}
关于#1楼曾向秋的评论
如果按照曾向秋的方式在客户端调用,输出的结果是0,不是1,显然不是我们所期望的。之所以不是1是因为:当调用list.GetEnumerator()的时候,就会调用MyEnumeraor类的构造函数,而我们没有在构造函数中让集合的指针自增1。
public MyEnumeraor(MyList<T> myList)
{_myList = myList;}
现在,我们需要在MyEnumeraor类的构造函数,让集合的指针自增1,并且需要把index变量作为静态字段放在MyList类下。
public class MyList<T> : IEnumerable<T>{T[] items = new T[5];
private int count;private static int index = -1;public void Add(T item){if(count == items.Length)
Array.Resize(ref items, items.Length * 2);
items[count++] = item;}public IEnumerator<T> GetEnumerator()
{return new MyEnumeraor(this);}class MyEnumeraor : IEnumerator<T>
{private MyList<T> _myList;
public MyEnumeraor(MyList<T> myList)
{index++;_myList = myList;}public T Current
{get
{if (index < 0 || index >= _myList.count)
{return default(T);}return _myList.items[index];
}}public void Dispose(){}object IEnumerator.Current
{get { return Current; }}public bool MoveNext(){return ++index < _myList.count;
}public void Reset(){index = -1;}}IEnumerator IEnumerable.GetEnumerator(){return GetEnumerator();
}}
然后,客户端就可以想像曾向秋所说的那样调用了。
static void Main(string[] args){MyList<int> list = new MyList<int>();list.Add(1);Console.WriteLine(list.GetEnumerator().Current);list.Add(2);Console.WriteLine(list.GetEnumerator().Current);Console.ReadKey();}
相关文章
- selenium遍历控件集合
- Python递归文件夹遍历所有文件夹及文件
- Java 集合List、Set、HashMap操作二(Map遍历、List反向、Set删除指定元素,集合只读、TreeMap操作、List转Array、List移动元素)
- 【算法】【链表模块】二叉树空间复杂度为O(1)的遍历方法(Morris算法)
- 【学习笔记23】JavaScript数组的遍历方法
- Google Earth Engine——如何下载里利用evaluate函数和for循环叠加使用遍历所有单景影像并导出(ui.button)
- 横着打印二叉树的形状,形状真的和二叉树一样,而不是遍历二叉树
- 1155 Heap Paths (30 分)【难度: 一般 / 知识点: 堆 堆的遍历】
- [转]使用Enumeration和Iterator遍历集合类
- nodejs遍历文件夹下并操作HTML/CSS/JS/PNG/JPG
- Python 用 os.walk 遍历目录
- 二叉树的遍历
- 遍历map的四种方法
- Java集合篇:Map常用遍历方式 以及 性能对比
- 92、【树与二叉树】leetcode ——111. 二叉树的最小深度:层次遍历+先序DFS+后序DFS[子问题分解](C++版本)
- 23、【数组】对角线遍历(C++版)
- 2022&2023华为OD机试 - 二叉树层次遍历(Python)
- C语言递归实现二叉树的先序、中序、后序遍历
- python第三十一课--递归(2.遍历某个路径下面的所有内容)
- [LeetCode] 314. Binary Tree Vertical Order Traversal 二叉树的竖直遍历
- 遍历文件夹