SortedList.RemoveAt(Int32) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
移除 SortedList 对象的指定索引处的元素。
public:
virtual void RemoveAt(int index);
public virtual void RemoveAt (int index);
abstract member RemoveAt : int -> unit
override this.RemoveAt : int -> unit
Public Overridable Sub RemoveAt (index As Integer)
参数
- index
- Int32
要移除的元素的从零开始的索引。
例外
index
不在 SortedList 对象的有效索引范围内。
示例
下面的代码示例演示如何从 SortedList 对象中删除元素。
#using <system.dll>
using namespace System;
using namespace System::Collections;
void PrintKeysAndValues( SortedList^ myList )
{
Console::WriteLine( "\t-KEY-\t-VALUE-" );
for ( int i = 0; i < myList->Count; i++ )
{
Console::WriteLine( "\t{0}:\t{1}", myList->GetKey( i ), myList->GetByIndex( i ) );
}
Console::WriteLine();
}
int main()
{
// Creates and initializes a new SortedList.
SortedList^ mySL = gcnew SortedList;
mySL->Add( "3c", "dog" );
mySL->Add( "2c", "over" );
mySL->Add( "1c", "brown" );
mySL->Add( "1a", "The" );
mySL->Add( "1b", "quick" );
mySL->Add( "3a", "the" );
mySL->Add( "3b", "lazy" );
mySL->Add( "2a", "fox" );
mySL->Add( "2b", "jumps" );
// Displays the SortedList.
Console::WriteLine( "The SortedList initially contains the following:" );
PrintKeysAndValues( mySL );
// Removes the element with the key "3b".
mySL->Remove( "3b" );
// Displays the current state of the SortedList.
Console::WriteLine( "After removing \"lazy\":" );
PrintKeysAndValues( mySL );
// Removes the element at index 5.
mySL->RemoveAt( 5 );
// Displays the current state of the SortedList.
Console::WriteLine( "After removing the element at index 5:" );
PrintKeysAndValues( mySL );
}
/*
This code produces the following output.
The SortedList initially contains the following:
-KEY- -VALUE-
1a: The
1b: quick
1c: brown
2a: fox
2b: jumps
2c: over
3a: the
3b: lazy
3c: dog
After removing "lazy":
-KEY- -VALUE-
1a: The
1b: quick
1c: brown
2a: fox
2b: jumps
2c: over
3a: the
3c: dog
After removing the element at index 5:
-KEY- -VALUE-
1a: The
1b: quick
1c: brown
2a: fox
2b: jumps
3a: the
3c: 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( "3c", "dog" );
mySL.Add( "2c", "over" );
mySL.Add( "1c", "brown" );
mySL.Add( "1a", "The" );
mySL.Add( "1b", "quick" );
mySL.Add( "3a", "the" );
mySL.Add( "3b", "lazy" );
mySL.Add( "2a", "fox" );
mySL.Add( "2b", "jumps" );
// Displays the SortedList.
Console.WriteLine( "The SortedList initially contains the following:" );
PrintKeysAndValues( mySL );
// Removes the element with the key "3b".
mySL.Remove( "3b" );
// Displays the current state of the SortedList.
Console.WriteLine( "After removing \"lazy\":" );
PrintKeysAndValues( mySL );
// Removes the element at index 5.
mySL.RemoveAt( 5 );
// Displays the current state of the SortedList.
Console.WriteLine( "After removing the element at index 5:" );
PrintKeysAndValues( mySL );
}
public static void PrintKeysAndValues( SortedList myList ) {
Console.WriteLine( "\t-KEY-\t-VALUE-" );
for ( int i = 0; i < myList.Count; i++ ) {
Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
The SortedList initially contains the following:
-KEY- -VALUE-
1a: The
1b: quick
1c: brown
2a: fox
2b: jumps
2c: over
3a: the
3b: lazy
3c: dog
After removing "lazy":
-KEY- -VALUE-
1a: The
1b: quick
1c: brown
2a: fox
2b: jumps
2c: over
3a: the
3c: dog
After removing the element at index 5:
-KEY- -VALUE-
1a: The
1b: quick
1c: brown
2a: fox
2b: jumps
3a: the
3c: dog
*/
Imports System.Collections
Public Class SamplesSortedList
Public Shared Sub Main()
' Creates and initializes a new SortedList.
Dim mySL As New SortedList()
mySL.Add("3c", "dog")
mySL.Add("2c", "over")
mySL.Add("1c", "brown")
mySL.Add("1a", "The")
mySL.Add("1b", "quick")
mySL.Add("3a", "the")
mySL.Add("3b", "lazy")
mySL.Add("2a", "fox")
mySL.Add("2b", "jumps")
' Displays the SortedList.
Console.WriteLine("The SortedList initially contains the following:")
PrintKeysAndValues(mySL)
' Removes the element with the key "3b".
mySL.Remove("3b")
' Displays the current state of the SortedList.
Console.WriteLine("After removing ""lazy"":")
PrintKeysAndValues(mySL)
' Removes the element at index 5.
mySL.RemoveAt(5)
' Displays the current state of the SortedList.
Console.WriteLine("After removing the element at index 5:")
PrintKeysAndValues(mySL)
End Sub
Public Shared Sub PrintKeysAndValues(myList As SortedList)
Console.WriteLine(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}", myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' The SortedList initially contains the following:
' -KEY- -VALUE-
' 1a: The
' 1b: quick
' 1c: brown
' 2a: fox
' 2b: jumps
' 2c: over
' 3a: the
' 3b: lazy
' 3c: dog
'
' After removing "lazy":
' -KEY- -VALUE-
' 1a: The
' 1b: quick
' 1c: brown
' 2a: fox
' 2b: jumps
' 2c: over
' 3a: the
' 3c: dog
'
' After removing the element at index 5:
' -KEY- -VALUE-
' 1a: The
' 1b: quick
' 1c: brown
' 2a: fox
' 2b: jumps
' 3a: the
' 3c: dog
注解
索引序列基于排序序列。 添加元素时,它将按正确的排序顺序插入到 SortedList 中,索引会相应地调整。 删除元素时,索引也会相应地进行调整。 因此,在对象中添加或移除元素时,特定键/值对的 SortedList 索引可能会更改。
在由连续的元素组成的集合(如列表)中,已移除元素下面的元素将上移以占据空出的位置。 如果集合具有索引,则移动的元素的索引也将更新。 此行为不适用于元素按概念划分为不同存储桶的集合,如哈希表。
此方法是一个 O(n)
操作,其中 n
是 Count。