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

张三

72

18

2009002

李四

88

19

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();
}
}
}