SortedList.Add(Object, Object) 方法

定義

將具有指定索引鍵和值的元素加入至 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 -> unit
Public Overridable Sub Add (key As Object, value As Object)

參數

key
Object

要加入的項目的索引鍵。

value
Object

要加入的項目的值。 這個值可以是 null

實作

例外狀況

keynull

SortedList 物件中己經包含具有指定之 key 的元素。

-或-

SortedList 設定為使用 IComparable 介面,而且 key 沒有實作 IComparable 介面。

SortedList 為唯讀。

-或-

SortedList 具有固定的大小。

沒有足夠的記憶體可用,無法將元素加入至 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 所指定的特定實作,或根據 IComparable 索引鍵本身提供的實作,依索引鍵本身所指定的特定 IComparer 實作來排序。

索引鍵不能是 null ,但值可以是 。

這個方法是 O(n) 未排序資料的作業,其中 nCount 。 如果在清單結尾加入新元素,則為 O(log n) 作業。 如果插入會導致調整大小,作業會是 O(n)

適用於

另請參閱