一个伪泛型List的实现
2012-04-29 22:34
375 查看
// 从一个数组的实现衍生出来的,说是伪泛型,是因为比较的时候是用ToString来比较的,所以只对极少部分的类型有用,真要用到的话,最好限制一下T的范围
using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { public class CList<T> { /// <summary> /// 数组增值数 /// </summary> public const int IncLength = 20; /// <summary> /// 字符串数组 /// </summary> protected T[] list; /// <summary> /// 字符串个数 /// </summary> protected int count; /// <summary> /// 空参构造方法 /// </summary> public CList() { this.list = new T[IncLength]; this.count = 0; } /// <summary> /// 以任意个(小于20)字符串参数构造对象的构造方法 /// </summary> /// <param name="items">变长形参</param> public CList(params T[] items) { list = new T[IncLength]; this.count = 0; if (items != null && items.Length > 0) { int maxLength = 0; if (items.Length > IncLength) { maxLength = IncLength; } else { maxLength = items.Length; } for (int i = 0; i < maxLength; i++) { this.list[i] = items[i]; } this.count = maxLength; } } /// <summary> /// 在不改变集合当前内容的情况下,使集合容量增加IncLength /// </summary> protected void Expend() { int newLength = this.list.Length + IncLength; T[] newList = new T[newLength]; for (int i = 0; i < this.count; i++) { newList[i] = this.list[i]; } this.list = null; this.list = newList; } /// <summary> /// 只读属性,返回当前集合中的元素个数 /// </summary> public int Count { get { return this.count; } } /// <summary> /// 在集合尾部追加一个元素,若失败抛出Exception /// </summary> /// <param name="item">新加入的元素</param> virtual public void Add(T item) { if (this.count == this.list.Length) { this.Expend(); } this.list[this.count] = item; this.count++; } /// <summary> /// 在集合尾部追加任意个元素,若失败抛出Exception /// </summary> /// <param name="items">变长形参</param> virtual public void Add(params T[] items) { if (this.count + items.Length > this.list.Length) { this.Expend(); } for (int i = 0; i < items.Length; i++) { this.list[this.count + i] = items[i]; } this.count += items.Length; } /// <summary> /// 在集合指定位置插入一个元素,若失败抛出Exception /// </summary> /// <param name="item">被插入的元素</param> /// <param name="index">插入位置</param> virtual public void Insert(T item, int index) { if (index < 0) { throw new Exception("索引值小于零。"); } if (this.count == this.list.Length) { this.Expend(); } if (index > this.count) { throw new Exception("索引值超出数组长度。"); } for (int i = this.count; i > index; i--) { this.list[i] = this.list[i - 1]; } this.list[index] = item; this.count++; } /// <summary> /// 移除指定位置的元素,若失败抛出Exception /// </summary> /// <param name="Index"></param> public void RemoveAt(int Index) { if (Index < 0) { throw new Exception("索引值小于零。"); } if (Index >= this.count) { throw new IndexOutOfRangeException("索引值大于数组长度。"); } for (int i = Index; i < this.count - 1; i++) { this.list[i] = this.list[i + 1]; } this.list[this.count - 1] = default(T); this.count--; } /// <summary> /// 清空集合所有元素 /// </summary> public void Clear() { this.count = 0; this.list = new T[IncLength]; } /// <summary> /// 查找指定字符串aString在集合中的位置(大小写敏感) /// </summary> /// <param name="item">被查找的字符串</param> /// <returns>找到则返回对应位置,未找到返回-1</returns> virtual public int Find(T t) { for (int i = 0; i < this.count; i++) { if (this.list[i] != null && this.list[i].Equals(t)) { return i; } } return -1; } /// <summary> /// 对集合进行排序 /// </summary> /// <param name="Opt">参数=asc为正向排序,=desc为逆向排序</param> public void Sort(string Opt = "asc") { if (Opt.ToLower() == "asc") { for (int i = 0; i < count; i++) { for (int j = i + 1; j < count; j++) { if (this.list[i].ToString().CompareTo(this.list[j].ToString()) > 0) { T temp; temp = this.list[i]; this.list[i] = this.list[j]; this.list[j] = temp; } } } } else { for (int i = 0; i < count; i++) { for (int j = i + 1; j < count; j++) { if (this.list[i].ToString().CompareTo(this.list[j].ToString()) < 0) { T temp; temp = this.list[i]; this.list[i] = this.list[j]; this.list[j] = temp; } } } } } /// <summary> /// 只读属性,返回集合中字符串长度最大的一项,若存在多项长度相等返回其中的第一项 /// </summary> public T MaxLenItem { get { if (this.count == 0) { return default(T); } if (this.count == 1) { return this.list[0]; } int index = 0; for (int i = 1; i < this.count; i++) { if (this.list[i].ToString().Length > this.list[i - 1].ToString().Length) { index = i; } } return this.list[index]; } } /// <summary> /// 只读属性,返回集合中字符串长度最小的一项,若存在多项长度相等返回其中的第一项 /// </summary> public T MinLenItem { get { if (this.count == 0) { return default(T); } if (this.count == 1) { return this.list[0]; } int index = 0; for (int i = 1; i < this.count; i++) { if (this.list[i].ToString().Length < this.list[i - 1].ToString().Length) { index = i; } } return this.list[index]; } } /// <summary> /// 按位置输出集合中的各项元素,输出格式为: /// /// 集合中共有?个元素,分别为: /// 元素1 /// 元素2 /// …… /// 元素n /// </summary> public void Show() { if (this.count == 0) { Console.WriteLine("集合中没有元素。"); return; } Console.WriteLine("集合中共有{0}个元素,分别为:", this.count); for (int i = 0; i < this.count; i++) { Console.WriteLine("元素{0}:{1}", i, this.list[i]); } } } public class CMyDirectory<T> : CList<T> { /// <summary> /// 检查字符串数组参数strArr中是否包含重复数据项(大小写不敏感) /// </summary> /// <param name="strArr">被检查的字符串数据</param> /// <returns>true:不包含重复项;false:包含重复项</returns> public bool IsValid(T[] strArr) { if (strArr.Length <= 1) { return false; } List<T> list = new List<T>(); list.Add(strArr[0]); for (int i = 1; i < strArr.Length; i++) { foreach (var s in list) { if (s.ToString().ToLower() == strArr[i].ToString().ToLower()) { return true; } } list.Add(strArr[i]); } return false; } /// <summary> /// 集合尾部添加一个新元素,且不能和已有元素重复(大小写不敏感) /// </summary> /// <param name="item">追加的新元素</param> public override void Add(T item) { bool contained = false; if (this.count == this.list.Length) { this.Expend(); } for (int i = 0; i < this.count; i++) { if (this.list[i].ToString().ToLower() == item.ToString().ToLower()) { contained = true; break; } } if (!contained) { base.Add(item); } } /// <summary> /// 集合尾部添加若干个新元素,且不能和已有元素重复(大小写不敏感) /// </summary> /// <param name="items">追加的新元素</param> public override void Add(params T[] items) { if (this.count + items.Length > this.list.Length) { this.Expend(); } for (int i = 0; i < items.Length; i++) { if (this.count == 0) { base.Add(items[i]); continue; } bool contained = false; for (int j = 0; j < this.count; j++) { if (this.list[j].ToString().ToLower() == items[i].ToString().ToLower()) { contained = true; break; } } if (!contained) { base.Add(items[i]); } } } /// <summary> /// 在指定的位置插入一个新元素,且不能和已有元素重复(大小写不敏感) /// </summary> /// <param name="item">插入的新元素</param> /// <param name="index">插入位置</param> public override void Insert(T item, int index) { bool contained = false; if (this.count == this.list.Length) { this.Expend(); } for (int i = 0; i < this.count; i++) { if (this.list[i].ToString().ToLower() == item.ToString().ToLower()) { contained = true; break; } } if (!contained) { base.Insert(item, index); } } /// <summary> /// 查找一个指定元素在集合中的位置(大小写不敏感)。 /// </summary> /// <param name="t"></param> /// <returns></returns> public override int Find(T t) { for (int i = 0; i < this.count; i++) { if (this.list[i].ToString().ToLower() == t.ToString().ToLower()) { return i; } } return -1; } } }
相关文章推荐
- 自己实现一个泛型LinkedList
- C#使用泛型数组简单实现一个列表List
- Python代码实现:删除一个list里面的重复元素
- 如何实现Java中一个简单的LinkedList
- 注解+反射+JDBC,实现一个简易的泛型DAO接口
- 一个用泛型简单实现的数据库查询实体方法
- java java local cache本地缓存的两种实现,一个基于list轮询一个基于timer定时
- 泛型实现中没有正确lock引用类型的一个隐藏bug分析
- 使用go语言的list实现一个简单的LRU缓存
- 将一个DataTable转换成一个List<T>的泛型集合
- 一个关于泛型list的警告
- 实现一个基于LinkedList的队列数据结构,去除ArrayList集合中重复的元素,
- System.Collections.Generic.List<>是System.Collections.ArrayList的泛型实现
- 一个用Java实现的Windows资源管理器 - FileList.java
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对
- java实现List集合中获取上一个下一个元素
- list 泛型时出现的一个Bug
- 注解+反射+JDBC,实现一个简易的泛型DAO接口
- NOPI实现导入导出泛型List,支持自定义列
- python 用list,dic实现switch功能的一个奇葩现象