SortedList.IndexOfValue(Object) SortedList.IndexOfValue(Object) SortedList.IndexOfValue(Object) SortedList.IndexOfValue(Object) Method

정의

지정한 값이 SortedList 개체에서 맨 처음 발견되는 인덱스(0부터 시작)를 반환합니다.Returns the zero-based index of the first occurrence of the specified value in a SortedList object.

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

SortedList 개체에서 찾을 값입니다.The value to locate in the SortedList object. 값은 null이 될 수 있습니다.The value can be null.

반환

valueSortedList 개체에 있으면 value 매개 변수가 맨 처음 발견되는 인덱스(0부터 시작)이고, 그렇지 않으면 -1입니다.The zero-based index of the first occurrence of the value parameter, if value is found in the SortedList object; otherwise, -1.

예제

다음 코드 예제에는 키 또는 값의 인덱스를 확인 하는 방법을 보여 줍니다는 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
Imports System.Collections
Imports Microsoft.VisualBasic

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.

설명

인덱스 순서는 정렬 순서를 기반으로 합니다.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 object.

요소의 값을 SortedList 사용 하 여 지정 된 값과 비교할는 Equals 메서드.The values of the elements of the SortedList are compared to the specified value using the Equals method.

이 메서드는 한 선형 검색 따라서이 메서드는 O (n) 작업, 여기서 nCount합니다.This method uses a linear search; therefore, this method is an O(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.

적용 대상

추가 정보