C# List<T>的用法(附:索引器)

List<T>类

所属命名空间:System.Collections.Generic

publicclassList<T> : IList<T>,ICollection<T>, IEnumerable<T>, IList,ICollection, IEnumerable

List<T>类是 ArrayList 类的泛型等效类。该类使用大小可按需动态增加的数组实现 IList<T> 泛型接口。

 泛型的好处: 它为使用c#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类型转换,所以性能得到提高。

性能注意事项:在决定使用IList<T> 还是使用ArrayList类(两者具有类似的功能)时,记住IList<T> 类在大多数情况下执行得更好并且是类型安全的。如果对IList<T> 类的类型 T 使用引用类型,则两个类的行为是完全相同的。但是,如果对类型 T 使用值类型,则需要考虑实现和装箱问题。

用微软的话讲:“添加到ArrayList 中的任何引用或值类型都将隐式地向上强制转换为Object。如果项是值类型,则必须在将其添加到列表中时进行装箱操作,在检索时进行取消装箱操作。强制转换以及装箱和取消装箱操作都会降低性能;在必须对大型集合进行循环访问的情况下,装箱和取消装箱的影响非常明显。”

常用的操作List<T>的方法(属性)

Capatcity 属性获取容量的大小

Count 属性访问元素的个数

Add(T item); 添加元素

Insert(int index,T item); 插入元素

[index] 访问元素

RemoveAt(int index); 移除指定下标的元素

Remove(T item); 移除指定元素

IndexOf(T item) ;从下标0开始搜索指定的元素,并返回第一个匹配到的元素下标,没有找到就返回-1

LastIndexOf(T item);与上面相反从后面开始索引,返回最后一个匹配到的元素下标,没有找到就返回-1

Sort();对列表的元素从小到大的排序

自定义ArrayList

    class ArrayDemo<T>
    {
        T[] arr;
        private int count = 0;
        private int capcity = 0;
        /// <summary>
        /// 返回数组中存储的元素个数
        /// </summary>
        public int Count
        {
            get
            {
                return count;
            }
        }

        /// <summary>
        /// 返回数组的容量,分配内存空间是有关系
        /// </summary>
        public int Capcity
        {
            get { return capcity; }
        }

        public ArrayDemo(int capcity = 0)
        {
            this.capcity = capcity;
            arr = new T[capcity < 0 ? 0 : capcity];
            count = 0;
        }

        /// <summary>
        /// 在数组的尾部添加一个新的元素
        /// 例如:第一个添加就在数组的0下标,第二个添加就在数组的1下标的位置
        /// </summary>
        /// <param name="value"></param>
        public void Add(T value)
        {
            //当count==capcity的时候就表示原始容量已经满了
            //扩大容量=capcity*2;
            if (capcity == 0)
            {
                capcity = 4;
                arr = new T[capcity];
            }
            else if (capcity != 0 && count == capcity)
            {
                capcity = capcity * 2;

                T[] brr = arr;
                arr = new T[capcity];
                Array.Copy(brr, arr, count);

            }
            arr[count++] = value;
        }
        /// <summary>
        /// 删除指定下标的元素
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            if (index > count - 1)
            {
                Console.WriteLine("删除错误:数组下标越界");
            }
            else
            {
                for (var i = index; i < count - 1; i++)
                {

                    arr[i] = arr[i + 1];

                }
                arr[count - 1] = default(T);//将最后一个置为默认值
                count--;
            }
        }

        /// <summary>
        /// 删除第一个在数组中检索到的T类型的value元素
        /// </summary>
        /// <param name="value"></param>
        public void Remove(T value)
        {

            for (var i = 0; i < count; i++)
            {
                if (value.Equals(arr[i]))
                {
                    RemoveAt(i);
                    break;
                }
            }

        }

        /// <summary>
        /// (索引器)通过下标返回指定下表的元素
        /// </summary>
        /// <param name="index">下标</param>
        /// <returns>返回指定下标的元素,如果下标不存在就报错</returns>
        public T this[int index]
        {
            get
            {
                if (index < count && index >= 0)
                    return arr[index];
                throw new System.IndexOutOfRangeException();
            }


        }

        /// <summary>
        /// 从数组首位开始检索,并返回该元素第一次检索到的下标,否则返回-1
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public int IndexOf(T value)
        {
            for (var i = 0; i < count; i++)
            {
                if (value.Equals(arr[i]))
                {
                    return i;
                }
            }
            return -1;
        }

       
    }

索引器(Indexer) 

允许一个对象可以像数组一样被索引。
当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。
您可以使用数组访问运算符([ ])来访问该类的实例。

索引器(Indexer)可被重载。
索引器声明的时候也可带有多个参数,且每个参数可以是不同的类型。

public T this[int index]
        {
            get{
                if (index >= 0 && index < count)
                {
                    return arr[index];
                }
                throw new System.IndexOutOfRangeException();
			} 
        }

猜你喜欢

转载自blog.csdn.net/qq_42485607/article/details/81066767