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이 될 수 있습니다.

구현

예외

key이(가) null인 경우

지정한 key를 가진 요소가 이미 SortedList 개체에 있는 경우

또는

SortedListIComparable 인터페이스를 사용하도록 설정되었으나 keyIComparable 인터페이스를 구현하지 않는 경우

SortedList이 읽기 전용인 경우

또는

SortedList가 고정 크기입니다.

SortedList에 요소를 추가할 수 있는 메모리가 충분하지 않은 경우

비교자에서 예외를 throw하는 경우

예제

다음 코드 예제에서는 요소를 추가 하는 방법을 보여 주는 개체입니다 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 명시적으로 또는 기본적으로 선택한 비교자에 따라 결정됩니다.

가 이미 이CapacityCount 내부 배열을 자동으로 다시 할당하여 개체의 SortedList 용량이 증가하고 새 요소가 추가되기 전에 기존 요소가 새 배열에 복사됩니다.

개체에 Item[] 없는 SortedList 키의 값(예 myCollection["myNonexistentKey"] = myValue: )을 설정하여 속성을 사용하여 새 요소를 추가할 수도 있습니다. 그러나 지정된 키가 에 SortedList이미 있는 경우 속성을 설정 Item[] 하면 이전 값이 덮어씁니다. 반면, 메서드는 Add 기존 요소를 수정하지 않습니다.

개체의 SortedList 요소는 가 생성될 때 SortedList 지정된 특정 IComparer 구현에 따라 또는 키 자체에서 제공하는 구현에 IComparable 따라 키별로 정렬됩니다.

키는 일 수 없지만 null값은 일 수 있습니다.

이 메서드는 O(n) 정렬되지 않은 데이터에 대한 작업입니다. 여기서 n 는 입니다 Count. O(log n) 목록 끝에 새 요소가 추가되면 작업입니다. 삽입으로 인해 크기가 조정되면 작업은 입니다 O(n).

적용 대상

추가 정보