.net C# SortedList用法

// 创建一个SortedList对象
SortedList mySL = new SortedList();
mySL.Add("First", "Hello");

 

//获得指定索引处的键和值
int myIndex = 3;
Console.WriteLine("The key at index {0} is {1}.", myIndex, mySL1.GetKey(myIndex));
Console.WriteLine("The value at index {0} is {1}.", myIndex, mySL1.GetByIndex(myIndex));

 

IList myKeyList = mySL1.GetKeyList();
IList myValueList = mySL1.GetValueList();

for (int i = 0; i < mySL1.Count; i++)
Console.WriteLine("\t{0}\t{1}", myKeyList[i], myValueList[i]);

 

// 获得指定键的索引
int myKey = 2;
Console.WriteLine("The key \"{0}\" is at index {1}.", myKey, mySL2.IndexOfKey(myKey));

// 获得指定值的索引
String myValue = "three";
Console.WriteLine("The value \"{0}\" is at index {1}.", myValue, mySL2.IndexOfValue(myValue));

// 重新设置指定索引处的值
mySL2.SetByIndex(3, "III");
mySL2.SetByIndex(4, "IV");

 

//打印SortedList中的键和值
public static void PrintIndexAndKeysAndValues(SortedList myList)
{
Console.WriteLine("\t-INDEX-\t-KEY-\t-VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine("\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}

}

 

posted @ 2020-03-05 09:19  Bella.Li  阅读(306)  评论(2编辑  收藏  举报