C#学习 SortedList

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.");
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值