SortedList.IndexOfKey(Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回 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 物件中尋找的索引鍵。
傳回
如果在 SortedList 物件中找到 key
,則為 key
參數之以零起始的索引,否則為 -1。
例外狀況
key
為 null
。
比較子會擲回例外狀況。
範例
下列程式代碼範例示範如何判斷索引鍵或物件中的 SortedList 值。
#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索引鍵本身所提供的實作,依索引鍵本身排序。
索引序列是以排序順序為基礎。 加入專案時,它會以正確的排序順序插入 , SortedList 而且索引編製會據以調整。 拿掉專案時,索引也會據以調整。 因此,當新增或移除 SortedList專案時,特定索引鍵/值組的索引可能會變更。
這個方法使用二進位搜尋演算法;因此,這個方法是作業 O(log n)
,其中 n
是 Count。
從 .NET Framework 2.0 開始,這個方法會使用 集合的物件 Equals 和 CompareTo 方法item
來判斷專案是否存在。 在舊版的 .NET Framework 中,已使用 Equals 集合中 物件的 和 CompareTo 方法item
,來決定此判斷。