第二学期复习---(三)Collection

    class Student
    {
        public int Id;
        public string Name;

        public Student(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }

    class Test
    {
       
        static void Main(string[] args)
        {
            //建立hashtable
            Hashtable ht = new Hashtable();
            int flag = 0;

            try
            {
                //hashtable添加数据
                ht.Add(2,new Student(2,"李四"));
                ht.Add(3,new Student(3,"王五"));
                ht.Add(1,new Student(1,"张三"));
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.Message);
            }

            //利用ArrayList为键排序
            ArrayList al = new ArrayList(ht.Keys);
            al.Sort();

            //遍历器打印排序后的结果
            IDictionaryEnumerator ide = ht.GetEnumerator();
            while (ide.MoveNext())
            {
                Student s = (Student)ht[ide.Key];
                Console.WriteLine(s.Id + " " + s.Name);
            }

            //查找姓名
            Console.Write("/n请你输入要查找的学生姓名:");
            string name = Console.ReadLine();
            foreach (int skey in al)
            {
                Student s = (Student)ht[skey];
                if (s.Name == name)
                {
                    Console.WriteLine("查找结果:学号为" + s.Id);
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
            {
                Console.WriteLine("没有这个学生!");
            }

            //查找学号
            Console.Write("/n请你输入要查找的学生学号:");
            int id = Convert.ToInt32(Console.ReadLine());
            if (ht.ContainsKey(id))
            {
                Student s = (Student)ht[id];
                Console.WriteLine("查找结果:姓名为" + s.Name);
            }
            else
            {
                Console.WriteLine("没有这个学生!");
            }

            //结束
            Console.ReadLine();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值