SortedList.IndexOfKey(Object) SortedList.IndexOfKey(Object) SortedList.IndexOfKey(Object) SortedList.IndexOfKey(Object) Method

정의

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 Object Object Object

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

반환

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

예외

비교자에서 예외를 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 개체 키를 기준으로 정렬 됩니다 특정에 따라 하나 IComparer 구현 될 때 지정 되는 SortedList 생성 인지에 따라를 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.

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

컬렉션의 개체를 사용 하 여이 메서드는.NET Framework 2.0 부터는 Equals 하 고 CompareTo 메서드를 item 항목이 있는지 여부를 확인 하려면.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의 이전 버전을 사용 하 여이 결정 했습니다 합니다 EqualsCompareTo 의 메서드는 item 컬렉션의 개체에 대 한 매개 변수입니다.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.

적용 대상

추가 정보