泛型

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

namespace ConsoleApp1
{
    class Array1<E>
    {
        
        //
        private E[] data;
        private int N;
        public Array1(int capacity)
        {
            data = new E[capacity];
            N = 0;
        }
        //public Array1() : this(10);
        public Array1()
        {
            data = new E[10];
            N = 0;
        }

        public int Capacity
        {
            get { return data.Length; }
        }
        public int Count
        {
            get { return N; }
        }
        public bool IsEmpty
        {
            get { return N == 0; }
        }
        public void Add(int index, E e)
        {
            if (index < 0 || index > N)
                throw new ArgumentException("数组索引越界");
            if (N == data.Length)
                throw new ArgumentException("数组已满");
            for (int i = N - 1; i >= index; i--)

                data[i + 1] = data[i];

            data[index] = e;
            N++;
        }
        private E Get(int index)
        {
            if (index < 0 || index >= N)
                throw new ArgumentException("数组索引越界");
            return data[index];
        }
        public E GetFirst()
        {
            return Get(0);
        }
        public E GetLast()
        {
            return Get(N - 1);
        }
        public void AddLast(E e)
        {
            Add(N, e);
        }
        public void AddFirst(E e)
        {
            Add(0, e);
        }
        public override string ToString()
        {
            StringBuilder res = new StringBuilder();
            res.Append(string.Format("Array1: count={0} capacity{1}\n", N, data.Length));
            res.Append("[");
            for (int i = 0; i < N; i++)
            {
                res.Append(data[i]);
                if (i != N - 1)
                    res.Append("。");
            }
            res.Append("]");
            return res.ToString();
        }
        public bool Contains(E e)
        {
            for (int i = 0; i < N; i++)
            {
                if (data[i].Equals(e))
                    return true;
            }
            return false;
        }
        public E RemoveAt(int index)
        {
            if (index < 0 || index >= N)
                throw new ArgumentException("数组索引越界");
            E del = data[index];
            for (int i = index + 1; i < N - 1; i++)
                data[i - 1] = data[i];
            N--;
            data[N] = default(E);
            if (N == data.Length / 4)
                ResetCapacity(data.Length / 2);
            return del;
        }
        public E RemoveFirst()
        {
            return RemoveAt(0);
        }
        public void Set(int index,E newE)
        {
            if (index < 0 || index >= N)
                throw new ArgumentException("数组索引越界");
            data[index] = newE;
        }
        public E RemoveLast()
        {
            return RemoveAt(N - 1);
        }
        public int IndexOf(int e)
        {
            for (int i = 0; i < N; i++)
            {
                if (data[i].Equals(e))
                    return i;
            }
            return -1;
        }
        public void Remove(int e)
        {
            int index = IndexOf(e);
            if (index != -1)
                RemoveAt(index);
        }
        public void ResetCapacity(int newCapacity)
        {
            E[] newData = new E[newCapacity];
            for (int i = 0; i < N; i++)
                newData[i] = data[i];
            data = newData;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[10];
            for (int i = 0; i < 10; i++)
            {
                arr[i] = i;
                Console.Write(arr[i] + " ");
            }
            Console.ReadLine();
            ArrayList a = new ArrayList(10);
            for (int i = 0; i < 15; i++)
            {
                a.Add(i);
                Console.WriteLine(a[i] + " ");
            }
            Console.Read();
            //Array1 b = new Array1(20);
            //for (int j = 0; j < 10;  j++)
            //{
            //    b.AddLast(j);
            //}
            //b.AddFirst(66);
            //b.Add(2, 5);
            //b.Remove(2);

            //Console.WriteLine(b);
            //Console.Read();
            //int m = 10000000;
            //Stopwatch t1 = new Stopwatch();
            //Stopwatch t2 = new Stopwatch();
            //Stopwatch t3 = new Stopwatch();
            //Stopwatch t4 = new Stopwatch();
            //Console.WriteLine("测试类型对象int");
            //t1.Start();
            //List<int> l = new List<int>();
            //for (int i = 0; i < m ; i++)
            //{
            //    l.Add(i);//不发生装箱
            //    int x = l[i];//不发生拆箱
            //}
            //t1.Stop();
            //Console.WriteLine("List'time: "+t1.ElapsedMilliseconds + "ms");

            //t2.Start();
            //ArrayList a = new ArrayList();
            //for (int i = 0; i < m; i++)
            //{
            //    a.Add(i);//发生装箱
            //    int x = (int)a[i];//发生拆箱
            //}
            //t2.Stop();
            //Console.WriteLine("ArrayList'time: "+t2.ElapsedMilliseconds + "ms");

            //Console.WriteLine("测试引用类型对象string");
            //t3.Start();
            //List<string> l2 = new List<string>();
            //for (int i = 0; i < m; i++)
            //{
            //    l2.Add("X");//不发生装箱
            //    string x = l2[i];//不发生拆箱
            //}
            //t3.Stop();
            //Console.WriteLine("List'time: " + t3.ElapsedMilliseconds + "ms");

            //t4.Start();
            //ArrayList a2 = new ArrayList();
            //for (int i = 0; i < m; i++)
            //{
            //    a2.Add("X");//发生装箱
            //    string x = (string)a2[i];//发生拆箱
            //}
            //t4.Stop();
            //Console.WriteLine("ArrayList'time: " + t4.ElapsedMilliseconds + "ms");
            //Console.Read();
            int[] n = { 1, 2, 3, 4, 5, 6, 7 };
            Array1<int> a = new Array1<int>();
            for (int i = 0; i < n.Length; i++)
                a.AddLast(n[i]);
            Console.WriteLine(a);
            string[] s = { "a", "b", "c", "d" };
            Array1<string> a2 = new Array1<string>();
            for (int i = 0; i < s.Length; i++)
                a2.AddLast(s[i]);
            Console.WriteLine(a2);
            Console.Read();



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值