SortedList.Add(Object, Object) 메서드

정의

지정된 키와 값을 가진 요소를 개체에 SortedList 추가합니다.

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

추가할 요소의 키입니다.

value
Object

추가할 요소의 값입니다. 값은 .일 null수 있습니다.

구현

예외

keynull입니다.

지정된 key 요소가 개체에 SortedList 이미 있습니다.

-또는-

인터페이스 SortedList 를 사용하도록 IComparable 설정되며 key 인터페이스를 IComparable 구현하지 않습니다.

읽기 SortedList 전용입니다.

-또는-

크기가 SortedList 고정되어 있습니다.

에 요소를 추가할 수 있는 메모리가 부족합니다 SortedList.

비교자는 예외를 throw합니다.

예제

다음 코드 예제에서는 개체에 요소를 SortedList 추가하는 방법을 보여줍니다.

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 명시적으로 또는 기본적으로 선택한 비교자에 따라 결정됩니다.

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

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

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

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

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

적용 대상

추가 정보