SortedList.Remove(Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從 SortedList 物件中移除具有指定之索引鍵的元素。
public:
virtual void Remove(System::Object ^ key);
public virtual void Remove (object key);
abstract member Remove : obj -> unit
override this.Remove : obj -> unit
Public Overridable Sub Remove (key As Object)
參數
- key
- Object
要移除的項目索引鍵。
實作
例外狀況
key
為 null
。
範例
下列程式代碼範例示範如何從 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 會保持不變。 不會擲回任何例外狀況。
在相鄰元素的集合中,例如清單,接在移除的元素之後的元素會向上移動以佔用空出的位置。 如果集合具有索引,則移動之項目的索引也會更新。 集合的項目若在概念上群組成 Bucket (例如雜湊資料表),則不適用這項行為。
這個方法是作業 O(n)
,其中 n
是 Count。