SortedList.IndexOfValue(Object) 메서드

정의

개체에서 지정된 값이 처음 나타나는 인덱스(0부터 시작)를 SortedList 반환합니다.

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

매개 변수

value
Object

개체에서 찾을 값입니다 SortedList . 값은 .일 null수 있습니다.

반품

개체에 있는 경우 value 매개 변수가 처음 발견되는 value 인덱스(0부터 시작)이고, 그렇지 않으면 -1입니다SortedList.

예제

다음 코드 예제에서는 개체에서 키 또는 값 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 삽입되고 인덱싱이 적절하게 조정됩니다. 요소가 제거되면 인덱싱도 그에 따라 조정됩니다. 따라서 개체에서 요소가 추가되거나 제거되면 특정 키/값 쌍의 인덱스가 SortedList 변경될 수 있습니다.

요소의 값은 메서드를 SortedList 사용하여 Equals 지정된 값과 비교됩니다.

이 메서드는 선형 검색을 사용합니다. 따라서 이 메서드는 O(n) 작업입니다. 여기서 n 는 .입니다 Count.

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

적용 대상

추가 정보