SortedList.GetKey(Int32) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取 SortedList 对象的指定索引处的键。
public:
virtual System::Object ^ GetKey(int index);
public virtual object GetKey (int index);
abstract member GetKey : int -> obj
override this.GetKey : int -> obj
Public Overridable Function GetKey (index As Integer) As Object
参数
- index
- Int32
要获取的键的从零开始的索引。
返回
SortedList 对象的指定索引处的键。
例外
index
不在 SortedList 对象的有效索引范围内。
示例
下面的代码示例演示如何获取 对象中的一个 SortedList 或所有键或值。
#using <system.dll>
using namespace System;
using namespace System::Collections;
int main()
{
// Creates and initializes a new SortedList.
SortedList^ mySL = gcnew SortedList;
mySL->Add( 1.3, "fox" );
mySL->Add( 1.4, "jumps" );
mySL->Add( 1.5, "over" );
mySL->Add( 1.2, "brown" );
mySL->Add( 1.1, "quick" );
mySL->Add( 1.0, "The" );
mySL->Add( 1.6, "the" );
mySL->Add( 1.8, "dog" );
mySL->Add( 1.7, "lazy" );
// Gets the key and the value based on the index.
int myIndex = 3;
Console::WriteLine( "The key at index {0} is {1}.", myIndex, mySL->GetKey( myIndex ) );
Console::WriteLine( "The value at index {0} is {1}.", myIndex, mySL->GetByIndex( myIndex ) );
// Gets the list of keys and the list of values.
IList^ myKeyList = mySL->GetKeyList();
IList^ myValueList = mySL->GetValueList();
// Prints the keys in the first column and the values in the second column.
Console::WriteLine( "\t-KEY-\t-VALUE-" );
for ( int i = 0; i < mySL->Count; i++ )
Console::WriteLine( "\t{0}\t{1}", myKeyList[ i ], myValueList[ i ] );
}
/*
This code produces the following output.
The key at index 3 is 1.3.
The value at index 3 is fox.
-KEY- -VALUE-
1 The
1.1 quick
1.2 brown
1.3 fox
1.4 jumps
1.5 over
1.6 the
1.7 lazy
1.8 dog
*/
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.3, "fox" );
mySL.Add( 1.4, "jumps" );
mySL.Add( 1.5, "over" );
mySL.Add( 1.2, "brown" );
mySL.Add( 1.1, "quick" );
mySL.Add( 1.0, "The" );
mySL.Add( 1.6, "the" );
mySL.Add( 1.8, "dog" );
mySL.Add( 1.7, "lazy" );
// Gets the key and the value based on the index.
int myIndex=3;
Console.WriteLine( "The key at index {0} is {1}.", myIndex, mySL.GetKey( myIndex ) );
Console.WriteLine( "The value at index {0} is {1}.", myIndex, mySL.GetByIndex( myIndex ) );
// Gets the list of keys and the list of values.
IList myKeyList = mySL.GetKeyList();
IList myValueList = mySL.GetValueList();
// Prints the keys in the first column and the values in the second column.
Console.WriteLine( "\t-KEY-\t-VALUE-" );
for ( int i = 0; i < mySL.Count; i++ )
Console.WriteLine( "\t{0}\t{1}", myKeyList[i], myValueList[i] );
}
}
/*
This code produces the following output.
The key at index 3 is 1.3.
The value at index 3 is fox.
-KEY- -VALUE-
1 The
1.1 quick
1.2 brown
1.3 fox
1.4 jumps
1.5 over
1.6 the
1.7 lazy
1.8 dog
*/
Imports System.Collections
Public Class SamplesSortedList
Public Shared Sub Main()
' Creates and initializes a new SortedList.
Dim mySL As New SortedList()
mySL.Add(1.3, "fox")
mySL.Add(1.4, "jumps")
mySL.Add(1.5, "over")
mySL.Add(1.2, "brown")
mySL.Add(1.1, "quick")
mySL.Add(1.0, "The")
mySL.Add(1.6, "the")
mySL.Add(1.8, "dog")
mySL.Add(1.7, "lazy")
' Gets the key and the value based on the index.
Dim myIndex As Integer = 3
Console.WriteLine("The key at index {0} is {1}.", myIndex, _
mySL.GetKey(myIndex))
Console.WriteLine("The value at index {0} is {1}.", myIndex, _
mySL.GetByIndex(myIndex))
' Gets the list of keys and the list of values.
Dim myKeyList As IList = mySL.GetKeyList()
Dim myValueList As IList = mySL.GetValueList()
' Prints the keys in the first column and the values in the second column.
Console.WriteLine(ControlChars.Tab & "-KEY-" & ControlChars.Tab & _
"-VALUE-")
Dim i As Integer
For i = 0 To mySL.Count - 1
Console.WriteLine(ControlChars.Tab & "{0}" & ControlChars.Tab & _
"{1}", myKeyList(i), myValueList(i))
Next i
End Sub
End Class
' This code produces the following output.
'
' The key at index 3 is 1.3.
' The value at index 3 is fox.
' -KEY- -VALUE-
' 1 The
' 1.1 quick
' 1.2 brown
' 1.3 fox
' 1.4 jumps
' 1.5 over
' 1.6 the
' 1.7 lazy
' 1.8 dog
注解
索引序列基于排序序列。 添加元素时,它将按正确的排序顺序插入到 SortedList 中,索引会相应地调整。 删除元素时,索引也会相应地进行调整。 因此,在对象中添加或移除元素时,特定键/值对的 SortedList 索引可能会更改。
此方法是一个 O(1)
操作。