SortedList.IndexOfKey(Object) 메서드

정의

SortedList 개체의 지정된 키 인덱스(0부터 시작)를 반환합니다.Returns the zero-based index of the specified key in a SortedList object.

public:
 virtual int IndexOfKey(System::Object ^ key);
public virtual int IndexOfKey (object key);
abstract member IndexOfKey : obj -> int
override this.IndexOfKey : obj -> int
Public Overridable Function IndexOfKey (key As Object) As Integer

매개 변수

key
Object

SortedList 개체에서 찾을 키입니다.The key to locate in the SortedList object.

반환

keykey 개체에 있으면 SortedList 매개 변수의 인덱스(0부터 시작)이고, 그렇지 않으면 -1입니다.The zero-based index of the key parameter, if key is found in the SortedList object; otherwise, -1.

예외

keynull인 경우key is null.

비교자에는 예외가 throw 됩니다.The comparer throws an exception.

예제

다음 코드 예제에서는 SortedList 개체에서 키 또는 값의 인덱스를 확인 하는 방법을 보여 줍니다.The following code example shows how to determine the index of a key or a value in a SortedList object.

#using <system.dll>

using namespace System;
using namespace System::Collections;
void PrintIndexAndKeysAndValues( SortedList^ myList )
{
   Console::WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" );
   for ( int i = 0; i < myList->Count; i++ )
   {
      Console::WriteLine( "\t[{0}]:\t{1}\t{2}", i, myList->GetKey( i ), myList->GetByIndex( i ) );

   }
   Console::WriteLine();
}

int main()
{
   
   // Creates and initializes a new SortedList.
   SortedList^ mySL = gcnew SortedList;
   mySL->Add( 1, "one" );
   mySL->Add( 3, "three" );
   mySL->Add( 2, "two" );
   mySL->Add( 4, "four" );
   mySL->Add( 0, "zero" );
   
   // Displays the values of the SortedList.
   Console::WriteLine( "The SortedList contains the following values:" );
   PrintIndexAndKeysAndValues( mySL );
   
   // Searches for a specific key.
   int myKey = 2;
   Console::WriteLine( "The key \"{0}\" is at index {1}.", myKey, mySL->IndexOfKey( myKey ) );
   
   // Searches for a specific value.
   String^ myValue = "three";
   Console::WriteLine( "The value \"{0}\" is at index {1}.", myValue, mySL->IndexOfValue( myValue ) );
}

/*
This code produces the following output.

The SortedList contains the following values:
        -INDEX- -KEY-   -VALUE-
        [0]:    0       zero
        [1]:    1       one
        [2]:    2       two
        [3]:    3       three
        [4]:    4       four

The key "2" is at index 2.
The value "three" is at index 3.
*/
using System;
using System.Collections;
public class SamplesSortedList  {

   public static void Main()  {

      // Creates and initializes a new SortedList.
      SortedList mySL = new SortedList();
      mySL.Add( 1, "one" );
      mySL.Add( 3, "three" );
      mySL.Add( 2, "two" );
      mySL.Add( 4, "four" );
      mySL.Add( 0, "zero" );

      // Displays the values of the SortedList.
      Console.WriteLine( "The SortedList contains the following values:" );
      PrintIndexAndKeysAndValues( mySL );

      // Searches for a specific key.
      int myKey = 2;
      Console.WriteLine( "The key \"{0}\" is at index {1}.", myKey, mySL.IndexOfKey( myKey ) );

      // Searches for a specific value.
      String myValue = "three";
      Console.WriteLine( "The value \"{0}\" is at index {1}.", myValue, mySL.IndexOfValue( myValue ) );
   }

   public static void PrintIndexAndKeysAndValues( SortedList myList )  {
      Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" );
      for ( int i = 0; i < myList.Count; i++ )  {
         Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i) );
      }
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

The SortedList contains the following values:
    -INDEX-    -KEY-    -VALUE-
    [0]:    0    zero
    [1]:    1    one
    [2]:    2    two
    [3]:    3    three
    [4]:    4    four

The key "2" is at index 2.
The value "three" is at index 3.
*/ 
Imports System.Collections

Public Class SamplesSortedList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new SortedList.
        Dim mySL As New SortedList()
        mySL.Add(1, "one")
        mySL.Add(3, "three")
        mySL.Add(2, "two")
        mySL.Add(4, "four")
        mySL.Add(0, "zero")
        
        ' Displays the values of the SortedList.
        Console.WriteLine("The SortedList contains the " & _
           "following values:")
        PrintIndexAndKeysAndValues(mySL)
        
        ' Searches for a specific key.
        Dim myKey As Integer = 2
        Console.WriteLine("The key ""{0}"" is at index {1}.", myKey, _
           mySL.IndexOfKey(myKey))
        
        ' Searches for a specific value.
        Dim myValue As String = "three"
        Console.WriteLine("The value ""{0}"" is at index {1}.", myValue, _
           mySL.IndexOfValue(myValue))
    End Sub    
    
    
    Public Shared Sub PrintIndexAndKeysAndValues(myList As SortedList)
        Console.WriteLine(ControlChars.Tab & "-INDEX-" & 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}" & ControlChars.Tab & "{2}", i, 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 values:
'     -INDEX-    -KEY-    -VALUE-
'     [0]:    0    zero
'     [1]:    1    one
'     [2]:    2    two
'     [3]:    3    three
'     [4]:    4    four
'
' The key "2" is at index 2.
' The value "three" is at index 3.

설명

SortedList 개체의 요소는 SortedList를 만들 때 지정 된 특정 IComparer 구현에 따라 또는 키 자체에서 제공 하는 IComparable 구현에 따라 키를 기준으로 정렬 됩니다.The elements of a SortedList object are sorted by the keys either according to a specific IComparer implementation specified when the SortedList is created, or according to the IComparable implementation provided by the keys themselves.

인덱스 순서는 정렬 순서를 기반으로 합니다.The index sequence is based on the sort sequence. 요소를 추가 하면 올바른 정렬 순서로 SortedList에 삽입 되 고 인덱싱이 그에 따라 조정 됩니다.When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly. 요소가 제거 되 면 인덱싱도 그에 따라 조정 됩니다.When an element is removed, the indexing also adjusts accordingly. 따라서 SortedList에서 요소를 추가 하거나 제거 하면 특정 키/값 쌍의 인덱스가 변경 될 수 있습니다.Therefore, the index of a specific key/value pair might change as elements are added or removed from the SortedList.

이 메서드는 이진 검색 알고리즘을 사용 합니다. 따라서이 메서드는 n CountO(log n) 작업입니다.This method uses a binary search algorithm; therefore, this method is an O(log n) operation, where n is Count.

.NET Framework 2.0부터이 메서드는 item의 컬렉션 개체 EqualsCompareTo 메서드를 사용 하 여 항목이 있는지 여부를 확인 합니다.Starting with the .NET Framework 2.0, this method uses the collection's objects' Equals and CompareTo methods on item to determine whether item exists. 이전 버전의 .NET Framework에서는 컬렉션의 개체에 대 한 item 매개 변수의 EqualsCompareTo 메서드를 사용 하 여 이러한 결정을 수행 했습니다.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the item parameter on the objects in the collection.

적용 대상

추가 정보