C# ArrayList和Hashtable的使用

ArrayList和Hashtable各自重要的方法:

ArrayList 动态数组: ArrayList arr=new ArrayList(); 
添加: arr.Add(object value);
删除: arr.Remove(object value);
插入: arr.Insert(int index, object value);
判断是否含有: arr.Contains(object value); 

Hashtable哈希表:Keys键集合,Values值集合,Hashtable ht=new Hashtable();
添加: ht.Add(int key,object value);
删除: ht.Remove(int key);
判断是否包含某键: ht.ContainsKey(int key);
判断是否包含某值: ht.ContainsValue(object value);

例子:

实现学生对象信息的添加、删除(指定学号)和查询(所有)学生对象信息如:

学号姓名成绩年龄
2009001张三7218
2009002李四8819

ArrayList实现:

namespace MyArrayList
{
    class Program
    {
        class Student
        {
            public string Sno,Sname;
            public int Grade,Age;
            public Student(string sno, string sname, int grade, int age)
            {
                Sno = sno;
                Sname = sname;
                Grade = grade;
                Age = age;
            }
            public Student(){ }
            public string TellInfo()
            {
                return string.Format("学号:{0},姓名:{1},成绩:{2},年龄:{3}", Sno, Sname, Grade, Age);
            }
        }

        static Student FindStudent(ArrayList al, string no)
        {
            Student s = null;
            for (int i = 0; i < al.Count; i++)
            {
                if ((al[i] as Student).Sno == no)
                {
                    s = al[i] as Student;
                    break;
                }
            }
            return s;
        }
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();
            Student s = new Student("20090001", "张三", 72, 18);
            al.Add(s);
            s = new Student("20090002", "李四", 88, 19);
            al.Add(s);  //添加,删除,查询学生信息
            int intCom = 0;
            string no = "";
            do
            {
                Console.WriteLine("1表示添加学生信息,2表示删除信息,3表示查询,0退出,请输入:");
                intCom = int.Parse(Console.ReadLine());
                switch (intCom)
                {
                    case 1: //添加
                        s = null;
                        Console.WriteLine("请输入要添加的学生学号");
                        no = Console.ReadLine();
                        s = FindStudent(al, no);
                        if (s == null)
                        {
                            s = new Student();
                            s.Sno = no;
                            Console.WriteLine("姓名:");
                            s.Sname = Console.ReadLine();
                            Console.WriteLine("成绩:");
                            s.Grade = int.Parse(Console.ReadLine());

                            Console.WriteLine("年龄:");
                            s.Age = int.Parse(Console.ReadLine());
                            al.Add(s);

                        }
                        else
                        {
                            Console.WriteLine("已经有{0}学生,不能添加!",s.Sno);
                        }
                        break;
                    case 2://删除
                        s = null;
                        Console.WriteLine("请输入要删除的学生学号");
                        no = Console.ReadLine();
                        s = FindStudent(al, no);
                        if (s == null)
                        {
                            Console.WriteLine("无此人");
                        }
                        else
                        {
                            al.Remove(s);
                            Console.WriteLine("已经删除学生{0}", no);
                        }
                        break;
                    case 3://查询
                        s = null;
                        Console.WriteLine("请输入要查找的学生学号");
                        no = Console.ReadLine();
                        s = FindStudent(al, no);
                        if (s == null)
                        {
                            Console.WriteLine("无此人");
                        }
                        else
                        {
                            Console.WriteLine("查找信息: " + s.TellInfo());
                        }

                        break;
                }



            } while (intCom != 0);
            Console.WriteLine("Bye");
            Console.ReadKey();
        }
    }
}

Hashtable实现:

namespace MyHashtable
{
    class Program
    {
        class Student
        {
            public string Sno, Sname;
            public int Grade,Age;
            public Student(string sno, string sname, int grade, int age)
            {
                Sno = sno;
                Sname = sname;
                Grade = grade;
                Age = age;
            }
            public Student(){}
            public string TellInfo()
            {
                return string.Format("学号:{0},姓名:{1},成绩:{2},年龄:{3}", Sno, Sname, Grade, Age);
            }
        }

        static void Main(string[] args)
        {
            Hashtable h = new Hashtable();
            Student s = new Student("20090001", "张三", 72, 18);
            h.Add(s.Sno, s);
            s = new Student("20090002", "李四", 88, 19);
            h.Add(s.Sno, s);
            int intCom;
            string no;
            do{
                Console.WriteLine("1表示添加学生信息,2表示删除信息,3表示查询,0退出,请输入:");
                intCom=int.Parse(Console.ReadLine());
                switch(intCom){
                    case 1:
                        Console.WriteLine("请输入要添加的学生学号");
                        no = Console.ReadLine();
                        if(h.Contains(no)){
                            Console.WriteLine("已经有{0}学生,不能添加!",no);
                        }
                        else {
                            s = new Student();
                            s.Sno = no;
                            Console.WriteLine("请输入姓名:");
                            s.Sname = Console.ReadLine();
                            Console.WriteLine("请输入成绩:");
                            s.Grade = int.Parse(Console.ReadLine());
                            Console.WriteLine("请输入年龄:");
                            s.Age = int.Parse(Console.ReadLine());
                            h.Add(s.Sno,s);
                        }
                        break;
                    case 2:
                        Console.WriteLine("请输入要删除的学生学号");
                        no=Console.ReadLine();
                        if(h.Contains(no)){
                            h.Remove(no);
                            Console.WriteLine("已经删除学生{0}", no);
                        }
                        else {
                            Console.WriteLine("无此人");
                        }
                        break;
                    case 3:
                        Console.WriteLine("请输入要查询的学生学号");
                        no=Console.ReadLine();
                        if(h.Contains(no)){
                            s=(Student)h[no];
                            Console.WriteLine("查找信息:"+s.TellInfo());
                        }
                        else {
                            Console.WriteLine("无此人");
                        }
                        break;
                }
            } while (intCom != 0);
            Console.WriteLine("Bye");
            Console.ReadKey();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值