SortedList.Add(Object, Object) 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将带有指定键和值的元素添加到 SortedList 对象。
public:
 virtual void Add(System::Object ^ key, System::Object ^ value);public virtual void Add (object key, object value);public virtual void Add (object key, object? value);abstract member Add : obj * obj -> unit
override this.Add : obj * obj -> unitPublic Overridable Sub Add (key As Object, value As Object)参数
- key
- Object
要添加的元素的键。
- value
- Object
要添加的元素的值。 该值可以为 null。
实现
例外
              key 为 null。
没有足够的可用内存来将元素添加到 SortedList。
比较器引发异常。
示例
下面的代码示例演示如何向 对象添加元素 SortedList 。
#using <system.dll>
using namespace System;
using namespace System::Collections;
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();
}
int main()
{
   
   // Creates and initializes a new SortedList.
   SortedList^ mySL = gcnew SortedList;
   mySL->Add( "one", "The" );
   mySL->Add( "two", "quick" );
   mySL->Add( "three", "brown" );
   mySL->Add( "four", "fox" );
   
   // Displays the SortedList.
   Console::WriteLine(  "The SortedList contains the following:" );
   PrintKeysAndValues( mySL );
}
/* 
This code produces the following output.
The SortedList contains the following:
        -KEY-   -VALUE-
        four:   fox
        one:    The
        three:  brown
        two:    quick
*/
using System;
using System.Collections;
public class SamplesSortedList  {
   public static void Main()  {
      // Creates and initializes a new SortedList.
      SortedList mySL = new SortedList();
      mySL.Add( "one", "The" );
      mySL.Add( "two", "quick" );
      mySL.Add( "three", "brown" );
      mySL.Add( "four", "fox" );
      // Displays the SortedList.
      Console.WriteLine( "The SortedList contains the following:" );
      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.
The SortedList contains the following:
    -KEY-    -VALUE-
    four:    fox
    one:    The
    three:    brown
    two:    quick
*/
Imports System.Collections
Public Class SamplesSortedList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new SortedList.
        Dim mySL As New SortedList()
        mySL.Add("one", "The")
        mySL.Add("two", "quick")
        mySL.Add("three", "brown")
        mySL.Add("four", "fox")
        
        ' Displays the SortedList.
        Console.WriteLine("The SortedList contains the following:")
        PrintKeysAndValues(mySL)
    End Sub    
    
    Public Shared Sub PrintKeysAndValues(myList As SortedList)
        Console.WriteLine(ControlChars.Tab & "-KEY-" & ControlChars.Tab & _
           "-VALUE-")
        Dim i As Integer
        For i = 0 To myList.Count - 1
            Console.WriteLine(ControlChars.Tab & "{0}:" & ControlChars.Tab & _
               "{1}", myList.GetKey(i), myList.GetByIndex(i))
        Next i
        Console.WriteLine()
    End Sub
End Class
' This code produces the following output.
' 
' The SortedList contains the following:
'     -KEY-    -VALUE-
'     four:    fox
'     one:    The
'     three:    brown
'     two:    quick
注解
插入点是根据创建对象时选择的比较器确定的(显式的或默认的 SortedList )。
如果 Count 已等于 Capacity,则通过自动重新分配内部数组来增加对象的容量 SortedList ,并在添加新元素之前将现有元素复制到新数组。
还可以使用 Item[] 属性通过设置对象 (中 SortedList 不存在的键的值来添加新元素,例如, myCollection["myNonexistentKey"] = myValue) 。 但是,如果 指定的键已存在于 中 SortedList,设置 属性 Item[] 将覆盖旧值。 相反, Add 方法不修改现有元素。
对象的元素SortedList根据创建 时SortedList指定的特定IComparer实现或IComparable键本身提供的实现按键排序。
键不能是 null,但值可以是 。
此方法是 O(n) 针对未排序数据的操作,其中 n 为 Count。 如果在列表末尾添加新元素,则这是一个 O(log n) 操作。 如果插入导致重设大小,则操作为 O(n)。