一个伪泛型List的实现

// 从一个数组的实现衍生出来的,说是伪泛型,是因为比较的时候是用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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值