SortedList.IndexOfKey(Object) 메서드

정의

개체에서 지정된 키의 인덱스(0부터 시작하는 인덱스)를 SortedList 반환합니다.

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 .

반품

개체에 있으면 매개 변수 keykey 인덱스(0부터 시작하는 SortedList 인덱스)이고, 그렇지 않으면 -1입니다.

예외

keynull입니다.

비교자는 예외를 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( 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 따라 키별로 정렬됩니다.

인덱스 시퀀스는 정렬 시퀀스를 기반으로 합니다. 요소가 추가되면 올바른 정렬 순서로 SortedList 삽입되고 인덱싱이 적절하게 조정됩니다. 요소가 제거되면 인덱싱도 그에 따라 조정됩니다. 따라서 요소가 추가되거나 제거될 때 특정 키/값 쌍의 인덱스가 변경될 SortedList수 있습니다.

이 메서드는 이진 검색 알고리즘을 사용합니다. 따라서 이 메서드는 O(log n) 작업입니다. 여기서 n 는 .입니다 Count.

이 메서드는 컬렉션의 개체 Equals 와 메서드를 CompareTo 사용하여 존재하는지 여부를 item 확인 item 합니다.

적용 대상

추가 정보