Freigeben über


SortedList.Add-Methode

Fügt SortedList ein Element mit dem angegebenen Schlüssel und Wert hinzu.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable Sub Add ( _
    key As Object, _
    value As Object _
)
'Usage
Dim instance As SortedList
Dim key As Object
Dim value As Object

instance.Add(key, value)
public virtual void Add (
    Object key,
    Object value
)
public:
virtual void Add (
    Object^ key, 
    Object^ value
)
public void Add (
    Object key, 
    Object value
)
public function Add (
    key : Object, 
    value : Object
)

Parameter

  • key
    Der Schlüssel des hinzuzufügenden Elements.
  • value
    Der Wert des hinzuzufügenden Elements. Der Wert kann NULL (Nothing in Visual Basic) sein.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentNullException

key ist NULL (Nothing in Visual Basic).

ArgumentException

Ein Element mit dem angegebenen key ist bereits in SortedList vorhanden.

– oder –

SortedList ist auf die Verwendung der IComparable-Schnittstelle festgelegt, und key implementiert die IComparable-Schnittstelle nicht.

NotSupportedException

SortedList ist schreibgeschützt.

– oder –

SortedList hat eine feste Größe.

InvalidOperationException

Der Vergleich löst eine Ausnahme aus.

Hinweise

Die Einfügeposition wird anhand des Vergleichs bestimmt, der entweder explizit oder standardmäßig beim Erstellen der SortedList ausgewählt wurde.

Wenn der Wert von Count bereits gleich der Capacity ist, wird die Kapazität der SortedList durch automatisches Neureservieren des internen Arrays erhöht, und die vorhandenen Elemente werden in das neue Array kopiert, bevor das neue Element hinzugefügt wird.

Sie können die Item-Eigenschaft auch zum Hinzufügen neuer Elemente verwenden, indem Sie den Wert eines Schlüssels festlegen, der in SortedList nicht vorhanden ist, beispielsweise myCollection["myNonexistentKey"] = myValue. Wenn der angegebene Schlüssel jedoch bereits in SortedList vorhanden ist, wird der alte Wert beim Festlegen der Item-Eigenschaft überschrieben. Im Gegensatz dazu ändert die Add-Methode keine vorhandenen Elemente.

Die Elemente einer SortedList sind nach Schlüsseln sortiert: entweder entsprechend einer bestimmten, beim Erstellen der SortedList angegebenen IComparer-Implementierung, oder entsprechend der von den Schlüsseln selbst bereitgestellten IComparable-Implementierung.

Ein Schlüssel kann nicht NULL (Nothing in Visual Basic) sein, bei einem Wert ist dies hingegen möglich.

Diese Methode ist bei unsortierten Daten ein O(n)-Vorgang, wobei n für den Count steht. Wenn das neue Element am Ende der Liste hinzugefügt wird, handelt es sich um einen O(log n)-Vorgang. Wenn beim Einfügen die Größe geändert werden muss, handelt es sich um einen O (n)-Vorgang.

Beispiel

Im folgenden Beispiel wird gezeigt, wie SortedList Elemente hinzugefügt werden.

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

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
 
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
*/
#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
*/
import System.*;
import System.Collections.*;

public class SamplesSortedList
{
    public static void main(String[] args)
    {
        // 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);
    } //main

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

/* 
 This code produces the following output.
 
 The SortedList contains the following:
     -KEY-    -VALUE-
     four:    fox
     one:    The
     three:    brown
     two:    quick
 */

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0

Siehe auch

Referenz

SortedList-Klasse
SortedList-Member
System.Collections-Namespace
Item
IComparer-Schnittstelle
IComparable-Schnittstelle
Capacity