📄 sample41.cs
字号:
namespace apiBook
{
using System;
using System.Collections;
public class TestSortedListClass
{
public static void Main()
{
TestSortedListClass t=new TestSortedListClass();
SortedList testSL = new SortedList();
testSL.Add( "1", "one" );
//使用Add方法添加元素
testSL.Add( "2", "two" );
testSL.Add( "3", "three" );
testSL.Add( "4", "four" );
testSL.Add( "5", "five" );
testSL.Add( "6", "six" );
// Displays the SortedList.
Console.WriteLine( "该SortedList包含的元素:" );
t.PrintValues( testSL );
Console.WriteLine("查看该对象里是否包含关键字3? "+testSL.Contains("3"));
//使用Contains方法查看该对象是否有以某值作为键
Console.WriteLine("查看该对象里是否包含关键字8? "+testSL.ContainsKey("8"));
//使用ContainsKey方法使用和Contains方法相同的功能
Console.WriteLine("查看该对象里是否包含值four? "+testSL.ContainsValue("three"));
//使用ContainsValue方法确定该对象是否有某个值
Array testArray=new Object[10]; //Array.CreateInstance(typeof(String),10);
testSL.Values.CopyTo(testArray,2);
//使用CopyTo方法将SortedList对象的值拷贝到数组
Console.WriteLine("将SortedList对象复制到一维Array对象该数组内容:");
t.PrintArrayValues(testArray, ',');
Console.WriteLine("将键赋到该数组。");
testSL.Keys.CopyTo(testArray,4);
//使用CopyTo方法拷贝键到数组
t.PrintArrayValues(testArray,',');
Console.WriteLine("该SortedList对象第5个元素值为:"+testSL.GetByIndex(4));
//使用GetByIndex方法获取某个位置的值
Console.WriteLine("该SortedList对象第2个元素的键是:"+testSL.GetKey(1));
//使用GetKey方法获取某个位置的键
IList testKeyList=testSL.GetKeyList();
//使用GetKeyList方法获取键的列表
IList testValueList=testSL.GetValueList();
//使用GetValueList方法获取值的列表
Console.WriteLine("该SortedList对象内容:");
for(int i=0;i<testSL.Count;i++)
Console.WriteLine(testKeyList[i]+" : "+testValueList[i]);
Console.WriteLine("更改第2和5个元素的值后:");
testSL.SetByIndex(2,"II");
//使用SetByIndex方法对某个位置的值进行修改或设置
testSL.SetByIndex(5,"VI");
t.PrintValues(testSL);
Console.WriteLine("键为6的位置是:"+testSL.IndexOfKey("6"));
//使用IndexOfKey方法查找某个键的索引
Console.WriteLine("值为one的位置是:"+testSL.IndexOfValue("one"));
//使用IndexOfValue方法查找某个值的索引
Console.WriteLine("删除键为4的元素后");
testSL.Remove("4");
//使用Remove方法删除某个键对应的元素
t.PrintValues(testSL);
Console.WriteLine("删除第2个元素后");
testSL.RemoveAt(2);
//使用RemoveAt方法删除某个位置的元素
t.PrintValues(testSL);
SortedList synSL=SortedList.Synchronized(testSL);
//使用Synchronized方法同步SortedList对象
Console.WriteLine("执行synList=SortedList.Synchronized(testSL)后");
Console.WriteLine("testSL是否被同步? "+testSL.IsSynchronized);
Console.WriteLine("synSL是否被同步? "+synSL.IsSynchronized);
Console.WriteLine("testSL的容量:"+testSL.Capacity);
Console.WriteLine("testSL的实际元素个数是:"+testSL.Count);
Console.WriteLine("执行TrimToSize操作后");
//使用TrimToSize方法压缩SortedList对象没有元素占的空间
testSL.TrimToSize();
Console.WriteLine("testSL的容量:"+testSL.Capacity);
Console.WriteLine("testSL的实际元素个数是:"+testSL.Count);
Console.ReadLine();
}
public void PrintArrayValues( Array array, char separator )
{
IEnumerator testEnumerator = array.GetEnumerator();
//使用GetEnumerator方法
int i = 0;
int col = array.GetLength( array.Rank - 1 );
while ( testEnumerator.MoveNext() )
{
if ( i < col )
{
i++;
}
else
{
Console.WriteLine();
i = 1;
}
Console.Write( " "+testEnumerator.Current+separator );
}
Console.WriteLine();
}
public void PrintValues( SortedList sortedList )
{
Console.WriteLine( "关键字:值" );
for ( int i = 0; i < sortedList.Count; i++ )
{
Console.WriteLine( sortedList.GetKey(i) +" : "+sortedList.GetByIndex(i) );
}
Console.WriteLine();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -