SortedList Class[MSDN]

链接: http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.aspx

A SortedList element can be accessed by its key, like an element in any IDictionary implementation, or by its index, like an element in any IList implementation.

A SortedList object internally maintains two arrays to store the elements of the list; that is, one array for the keys and another array for the associated values. Each element is a key/value pair that can be accessed as a DictionaryEntry object. A key cannot be nullNothingnullptra null reference (Nothing in Visual Basic), but a value can be.

The capacity of a SortedList object is the number of elements the SortedList can hold. As elements are added to a SortedList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling TrimToSize or by setting the Capacity property explicitly.

The elements of a SortedList object are sorted by the keys either according to a specific IComparer implementation specified when the SortedList is created or according to the IComparable implementation provided by the keys themselves. In either case, a SortedList does not allow duplicate keys.

The index sequence is based on the sort sequence. When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly. When an element is removed, the indexing also adjusts accordingly. Therefore, the index of a specific key/value pair might change as elements are added or removed from the SortedList object.

Operations on a SortedList object tend to be slower than operations on a Hashtable object because of the sorting. However, the SortedList offers more flexibility by allowing access to the values either through the associated keys or through the indexes.

Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.

The foreach statement of the C# language (for each in Visual Basic) requires the type of each element in the collection. Since each element of the SortedList object is a key/value pair, the element type is not the type of the key or the type of the value. Rather, the element type is DictionaryEntry. For example:

foreach (DictionaryEntry de in mySortedList) {}
using  System;
using
 System.Collections;
public class SamplesSortedList  
{

   
public static void Main()  
{

      
// Creates and initializes a new SortedList.

      SortedList mySL = new SortedList();
      mySL.Add(
"First""Hello"
);
      mySL.Add(
"Second""World"
);
      mySL.Add(
"Third""!"
);

      
// Displays the properties and values of the SortedList.

      Console.WriteLine( "mySL" );
      Console.WriteLine( 
"  Count:    {0}"
, mySL.Count );
      Console.WriteLine( 
"  Capacity: {0}"
, mySL.Capacity );
      Console.WriteLine( 
"  Keys and Values:"
 );
      PrintKeysAndValues( mySL );
   }



   
public static void PrintKeysAndValues( SortedList myList )  {
      Console.WriteLine( 
"\t-KEY-\t-VALUE-"
 );
      
for ( int i = 0; i < myList.Count; i++ )  
{
         Console.WriteLine( 
"\t{0}:\t{1}"
, myList.GetKey(i), myList.GetByIndex(i) );
      }

      Console.WriteLine();
   }

}

/**/ /* 
This code produces the following output.

mySL
  Count:    3
  Capacity: 16
  Keys and Values:
    -KEY-    -VALUE-
    First:    Hello
    Second:    World
    Third:    !
*/
 

转载于:https://www.cnblogs.com/onlyendure/archive/2008/05/13/1194846.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值