ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#实现自定义列表

2020-05-18 22:56:06  阅读:373  来源: 互联网

标签:index mylist 自定义 C# 列表 int Console array public


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace  CustomizedList {
    class MyList<T> where T:IComparable
    {
        private T[] array;
        private int count=0;//表示当前添加的元素的个数

        public MyList(int size)
        {
            if (size >= 0)
            {
                array = new T[size];
            }
        }

        public MyList()
        {
            array = new T[0];
        }

        public int Capacity
        {
            get { return array.Length; }
        }

        public int Count
        {
            get { return count; }
        }

        public void Add(T item )
        {
            if (Count == Capacity) //判断元素个数跟列表容量大小是否一样大,如果一样大,说明数组容量不用,需要创建新的数组
            {
                if (Capacity == 0)
                {
                    array = new T[4];//当数组长度为0的时候,创建一个长度为4的数组
                }
                else
                {
                    var newArray = new T[Capacity*2];//当长度不为0的时候,我们创建一个长度为原来2倍的数组
                    Array.Copy(array,newArray,Count);//把旧数组中的元素复制到新的数组中 , 复制前count个  array-->newArray
                    array = newArray;
                }
            }
            array[Count] = item;
            count++;//元素个数自增
        }

        public T GetItem(int index)
        {
            if (index >= 0 && index <= count - 1)
            {
                return array[index];
            }
            else
            {
                //Console.WriteLine("索引超出了范围");
                throw new Exception("索引超出了范围");
            }
        }

        public T this[int index]
        {
            get//当我们通过索引器取值的时候,会调用get块
            { return GetItem(index); }
            set//当我们通过索引器设置值的时候,会调用set块
            {
                if (index >= 0 && index <= count - 1)
                {
                    array[index] = value;
                } else {
                    //Console.WriteLine("索引超出了范围");
                    throw new Exception("索引超出了范围");
                }
            }
        }

        public void Insert(int index, T item)
        {
            if (index >= 0 && index <= count - 1)
            {
                if (Count == Capacity)//容量不够 进行扩容
                {
                    var newArray = new T[Capacity*2];   
                    Array.Copy(array,newArray,count);
                    array = newArray;//newArray被GC机制回收
                }
                for (int i = count-1; i >=index; i--)
                {
                    array[i + 1] = array[i];//把i位置的值放在后面,就是向后移动一个单位

                }
                array[index] = item;
                count++;
            }
            else
            {
                throw new Exception("所以超出范围");
            }
        }

        public void RemoveAt(int index)
        {
            if (index >= 0 && index <= count - 1)
            {
                for (int i = index + 1; i < count; i++)
                {
                    array[i - 1] = array[i];
                }
                count--;
            }
            else
            {
                throw new Exception("索引超出范围");
            }
        }

        public int IndexOf(T item)
        {
            for (int i = 0; i < count; i++)
            {
                if (array[i].Equals(item))
                {
                    return i;
                }
            }
            return -1;
        }

        public int LastIndexOf(T item)
        {
            for (int i = Count-1; i >=0; i--) {
                if (array[i].Equals(item)) {
                    return i;
                }
            }
            return -1;
        }

        public void Sort()
        {
            for (int j = 0; j < Count-1; j++)
            {
                for (int i = 0; i < Count - 1 - j; i++) {
                    if (array[i].CompareTo(array[i + 1]) > 0) {
                        T temp = array[i];
                        array[i] = array[i + 1];
                        array[i + 1] = temp;
                    }
                }
            }
           
        }
    }




     //测试
     class Program {
        static void Main(string[] args) {
            
            MyList<int> mylist = new MyList<int>();
            mylist.Add(234);
            mylist.Add(14);
            mylist.Add(24);
            mylist.Add(24);
            mylist.Add(90);
            mylist.Add(274);
            //mylist[index]
            mylist.Insert(1,2);
            //mylist.RemoveAt(-1);
            mylist[0] = 100;//通过索引器 设置值
            for (int i = 0; i < mylist.Count; i++)
            {
                //Console.WriteLine(mylist.GetItem(i));
                Console.WriteLine(mylist[i]);//通过索引器 取值
            }
            Console.WriteLine(mylist.IndexOf(24));
            Console.WriteLine(mylist.LastIndexOf(24));
            mylist.Sort();
            Console.WriteLine();
            for (int i = 0; i < mylist.Count; i++) {
                //Console.WriteLine(mylist.GetItem(i));
                Console.WriteLine(mylist[i]);//通过索引器 取值
            }
            Console.ReadKey();
        }
    }
}


标签:index,mylist,自定义,C#,列表,int,Console,array,public
来源: https://www.cnblogs.com/ezhar/p/12913537.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有