SortedList.IndexOfValue(Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回 SortedList 物件中第一個出現指定值之以零起始的索引。
public:
virtual int IndexOfValue(System::Object ^ value);
public virtual int IndexOfValue(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。
傳回
參數首次出現valuevalue時的零基索引為 若在物件中,SortedList則為 -1。
範例
以下程式碼範例說明如何判定物件中 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。
此方法利用集合中的物件與 EqualsCompareTo 方法 item 來判斷是否 item 存在。