lwj_C#_集合的使用、接口方法的实现;

来源:互联网 发布:医疗器械软件关税 编辑:程序博客网 时间:2024/05/01 00:19
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace homework
{
                           //枚举能不能做//枚举如何实现
    public class MyList <T>:IEnumerable,IEnumerator{
        //创建集合字段要初始化
        public List<Tlist = new List<T>();
        private int currentIndex = 0;

        public IEnumerator GetEnumerator()
        {
            return this;
        }
        //先走MoveNext 再走Current 
        //移位
        public bool MoveNext()
        {
            if (currentIndex<list.Count)
            {
                return true;
            }
            Reset();
            return  false;
        }
        public object Current
        {
            get
            {
                Object obj = list[currentIndex];
                currentIndex++;
                return obj;
            }
        }
        //重置  是用来重置循环打印次数;
        public void Reset()
        {
            currentIndex = 0;
        }
    }
    class MainClass
    {
        public static void Main(string[] args)
        {
            #region // A类 //第一题: 哈希表和字典存取数据所消耗的时间对比
            //题目
            //解题思路
            //1.分别创建哈希表与字典
            //2.分别添加对应的数据使用stopwatch统计耗时
            //3.分别取对应的数据使用stopwatch统计耗时

            //添加数据比较;
            Hashtable hash = new Hashtable();
            Stopwatch stop_1 = new Stopwatch();
            stop_1.Start();
            hash.Add("jiangkai"10);
            stop_1.Stop();
            Console.WriteLine("哈希表的时间为:" + stop_1.Elapsed);

            Dictionary<stringintdic = new Dictionary<stringint>();
            Stopwatch stop_2 = new Stopwatch();
            stop_2.Start();
            dic.Add("jiangkai"10);
            stop_2.Stop();
            Console.WriteLine("字典的时间为:" + stop_2.Elapsed);


            //取数据比较;
            Stopwatch stop_3 = new Stopwatch();
            stop_3.Start();
            hash.Remove("jiangkai");
            stop_3.Stop();
            Console.WriteLine("哈希表的时间为:" + stop_3.Elapsed);

            Stopwatch stop_4 = new Stopwatch();
            stop_4.Start();
            dic.Remove("jiangklai");
            stop_4.Stop();
            Console.WriteLine("字典的时间为:" + stop_4.Elapsed);
            #endregion


            #region //第二题: 查询MSDN文档,练习ArrayList集合下面的常用API练习一遍.然后思考ArrayList能用于开发中(或者你接触过的游戏中)的那些地方? 至少写出两个, 并能分析出其实现方式.
            ArrayList arr = new ArrayList();
            arr.Add("lwj");
            arr.Add(12);
            arr.Add('a');
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }
            //指定下标插入元素;
            arr.Insert(1"niubi");
            Console.WriteLine("");
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }
            //是否包含一个指定元素
            bool isexit = arr.Contains(12);
            Console.WriteLine("\n" + isexit);
            //逆转
            arr.Reverse();
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }
            //移除指定下标元素;
            arr.RemoveAt(2);
            Console.WriteLine("");
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }
            //移除指定元素
            arr.Remove("lwj");
            Console.WriteLine("");
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }
            //移除所有元素
            arr.Clear();
            Console.WriteLine("");
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }
            //ArrayList能用于:1、游戏中的技能BUFF移除、添加等操作; 2、游戏物品栏的添加移除操作;

            #endregion


            //解题思路
            //1.自定义类拥有集合的作用,意味着类成员至少有一个为系统提供的集合
            //2.利用系统集合的方法,使用本类进行二次封装
            //3.类支持foreach循环,必须实现IEnumerable接口
            //4.利用里氏替换原则替换父类实现IEnumerator
            //5.创建测试类
            //6.利用自定类内部的索引器添加测试类对象
            //7.测试foreach循环
            int[] array = { 122323432322 };
            MyList<intmylist = new MyList<int>();
            mylist.list = new List<int>(array);
            foreach (var item in mylist)
            {
                Console.WriteLine(item);
            }



        }
    }
}
原创粉丝点击