SortedList是C#自带的一个数据结构,我的理解是SortedList相当于一个map,一一映射,假设一个条目是<key,course>,可以通过key查到这一条的index,然后调用GetByIndex(index)方法取得course的内容
创建一个SortedList
SortedList myCourses = new SortedList();
往SortedList里加入内容
static void populateList(SortedList list)
{
list.Add("CS101", "Introduction to Computer Science");
list.Add("CS102", "Data Structures and Algorithm Analysis");
list.Add("CS201", "Introduction to Databases");
list.Add("CS301", "Introduction to Object-Oriented Programming");
}
访问SortedList
static void displayList(SortedList list, string key)
{
int index;
string course;
index = list.IndexOfKey(key); //返回从零开始的索引
course = (string)list.GetByIndex(index);
Console.WriteLine(course);
}
删除SortedList中的条目
static void removeListItem(SortedList list, string key)
{
int index;
string course;
index = list.IndexOfKey(key);
course = (string)list.GetByIndex(index);
list.Remove(key);
Console.WriteLine(course + " was removed from the list.");
}