SortedList.Add(Object, Object) SortedList.Add(Object, Object) SortedList.Add(Object, Object) SortedList.Add(Object, Object) Method

定义

将带有指定键和值的元素添加到 SortedList 对象。Adds an element with the specified key and value to a SortedList object.

public:
 virtual void Add(System::Object ^ key, System::Object ^ value);
public virtual void Add (object key, object value);
abstract member Add : obj * obj -> unit
override this.Add : obj * obj -> unit
Public Overridable Sub Add (key As Object, value As Object)

参数

key
Object Object Object Object

要添加的元素的键。The key of the element to add.

value
Object Object Object Object

要添加的元素的值。The value of the element to add. 该值可以为 nullThe value can be null.

实现

异常

带有指定 key 的元素已经存在于 SortedList 对象中。An element with the specified key already exists in the SortedList object.

-or- SortedList 设置为使用 IComparable 接口,并且 key 不实现 IComparable 接口。The SortedList is set to use the IComparable interface, and key does not implement the IComparable interface.

SortedList 为只读。The SortedList is read-only.

-or- SortedList 具有固定的大小。The SortedList has a fixed size.

没有足够的可用内存来将元素添加到 SortedListThere is not enough available memory to add the element to the SortedList.

示例

下面的代码示例演示如何向SortedList对象添加元素。The following code example shows how to add elements to a SortedList object.

#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对象时确定的。The insertion point is determined based on the comparer selected, either explicitly or by default, when the SortedList object was created.

如果Count已相等Capacity ,则会自动重新分配内部数组,并将现有元素复制到新数组,然后再添加新元素。SortedListIf Count already equals Capacity, the capacity of the SortedList object is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.

你还可以使用Item[Object]属性来添加新元素, 方法是设置SortedList myCollection["myNonexistentKey"] = myValue对象中不存在的键的值 (例如)。You can also use the Item[Object] property to add new elements by setting the value of a key that does not exist in the SortedList object (for example, myCollection["myNonexistentKey"] = myValue). 但是, 如果指定的键已存在于中SortedList, 则Item[Object]设置属性会覆盖旧值。However, if the specified key already exists in the SortedList, setting the Item[Object] property overwrites the old value. 与此Add相反, 方法不会修改现有元素。In contrast, the Add method does not modify existing elements.

SortedList对象的元素根据创建时SortedList指定的特定IComparer实现进行排序, 或根据IComparable键本身提供的实现进行排序。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.

键不能为null, 但值可以为。A key cannot be null, but a value can be.

此方法对于未排序的n数据是 O () 运算, n其中Count是。This method is an O(n) operation for unsorted data, where n is Count. 如果在列表的末尾添加n新元素, 则它是一个 O (log) 操作。It is an O(log n) operation if the new element is added at the end of the list. 如果插入操作导致大小调整, 则操作为 On()。If insertion causes a resize, the operation is O(n).

适用于

另请参阅