SortedList.Add(Object, Object) Metoda

Definicja

Dodaje element z określonym kluczem i wartością SortedList do obiektu.

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)

Parametry

key
Object

Klucz elementu do dodania.

value
Object

Wartość elementu do dodania. Wartość może mieć wartość null.

Implementuje

Wyjątki

key to null.

Element z określonym key elementem już istnieje w SortedList obiekcie .

-lub-

Parametr SortedList ma używać interfejsu IComparable i key nie implementuje interfejsu IComparable .

Element SortedList jest tylko do odczytu.

-lub-

Element SortedList ma stały rozmiar.

Za mało dostępnej pamięci, aby dodać element do elementu SortedList.

Porównujący zgłasza wyjątek.

Przykłady

Poniższy przykład kodu przedstawia sposób dodawania SortedList elementów do obiektu.

#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

Uwagi

Punkt wstawiania jest określany na podstawie wybranego porównania, jawnie lub domyślnie, podczas SortedList tworzenia obiektu.

Jeśli Count już jest równa Capacity, pojemność SortedList obiektu jest zwiększana przez automatyczne przydzielenie tablicy wewnętrznej, a istniejące elementy są kopiowane do nowej tablicy przed dodaniu nowego elementu.

Możesz również użyć Item[] właściwości , aby dodać nowe elementy, ustawiając wartość klucza, który nie istnieje w SortedList obiekcie (na przykład myCollection["myNonexistentKey"] = myValue). Jeśli jednak określony klucz już istnieje w SortedListobiekcie , ustawienie Item[] właściwości zastępuje starą wartość. Z kolei Add metoda nie modyfikuje istniejących elementów.

Elementy SortedList obiektu są sortowane według kluczy zgodnie z określoną IComparer implementacją określoną podczas SortedList tworzenia lub zgodnie z implementacją IComparable dostarczaną przez same klucze.

Klucz nie może być nullwartością , ale może to być wartość.

Ta metoda jest operacją O(n) dla niesortowanych danych, gdzie n to Count. Jest to O(log n) operacja, jeśli nowy element zostanie dodany na końcu listy. Jeśli wstawienie powoduje zmianę rozmiaru, operacja to O(n).

Dotyczy

Zobacz też