SortedList.RemoveAt メソッド
SortedList の指定したインデックスにある要素を削除します。
Public Overridable Sub RemoveAt( _
ByVal index As Integer _)
[C#]
public virtual void RemoveAt(intindex);
[C++]
public: virtual void RemoveAt(intindex);
[JScript]
public function RemoveAt(
index : int);
パラメータ
- index
削除する要素の、0 から始まるインデックス番号。
例外
例外の種類 | 条件 |
---|---|
ArgumentOutOfRangeException | index が、 SortedList の有効なインデックスの範囲外です。 |
NotSupportedException | SortedList が読み取り専用です。
または SortedList が固定サイズです。 |
解説
並べ替え順に基づいたインデックス順。要素を追加すると、その要素は正しい並べ替え順に従って SortedList に挿入され、それに応じてインデックスも調整されます。要素が削除されると、それに応じてインデックスも調整されます。したがって、特定のキーと値の組み合わせのインデックスは、要素が SortedList で追加または削除されるときに変更されることがあります。
リストなどの連続する要素のコレクションでは、空白になった位置を埋めるために、削除された要素の後にある要素の位置が繰り上げられます。インデックス付きのコレクションの場合は、移動した要素のインデックスも更新されます。この動作は、要素が概念的にバケットにグループ化されているハッシュテーブルなどのコレクションには適用されません。
使用例
[Visual Basic, C#, C++] SortedList から要素を削除する方法の例を次に示します。
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
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", "jumped")
' 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: jumped
' 2c: over
' 3a: the
' 3b: lazy
' 3c: dog
'
' After removing "lazy":
' -KEY- -VALUE-
' 1a: The
' 1b: quick
' 1c: brown
' 2a: fox
' 2b: jumped
' 2c: over
' 3a: the
' 3c: dog
'
' After removing the element at index 5:
' -KEY- -VALUE-
' 1a: The
' 1b: quick
' 1c: brown
' 2a: fox
' 2b: jumped
' 3a: the
' 3c: dog
[C#]
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", "jumped" );
// 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: jumped
2c: over
3a: the
3b: lazy
3c: dog
After removing "lazy":
-KEY- -VALUE-
1a: The
1b: quick
1c: brown
2a: fox
2b: jumped
2c: over
3a: the
3c: dog
After removing the element at index 5:
-KEY- -VALUE-
1a: The
1b: quick
1c: brown
2a: fox
2b: jumped
3a: the
3c: dog
*/
[C++]
#using <mscorlib.dll>
#using <system.dll>
using namespace System;
using namespace System::Collections;
void PrintKeysAndValues( SortedList* myList ) {
Console::WriteLine( S"\t-KEY-\t-VALUE-" );
for ( int i = 0; i < myList->Count; i++ ) {
Console::WriteLine( S"\t{0}:\t{1}", myList->GetKey(i), myList->GetByIndex(i) );
}
Console::WriteLine();
}
int main() {
// Creates and initializes a new SortedList.
SortedList* mySL = new SortedList();
mySL->Add( S"3c", S"dog" );
mySL->Add( S"2c", S"over" );
mySL->Add( S"1c", S"brown" );
mySL->Add( S"1a", S"The" );
mySL->Add( S"1b", S"quick" );
mySL->Add( S"3a", S"the" );
mySL->Add( S"3b", S"lazy" );
mySL->Add( S"2a", S"fox" );
mySL->Add( S"2b", S"jumped" );
// Displays the SortedList.
Console::WriteLine( S"The SortedList initially contains the following:" );
PrintKeysAndValues( mySL );
// Removes the element with the key "3b".
mySL->Remove( S"3b" );
// Displays the current state of the SortedList.
Console::WriteLine( S"After removing \"lazy\":" );
PrintKeysAndValues( mySL );
// Removes the element at index 5.
mySL->RemoveAt( 5 );
// Displays the current state of the SortedList.
Console::WriteLine( S"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: jumped
2c: over
3a: the
3b: lazy
3c: dog
After removing "lazy":
-KEY- -VALUE-
1a: The
1b: quick
1c: brown
2a: fox
2b: jumped
2c: over
3a: the
3c: dog
After removing the element at index 5:
-KEY- -VALUE-
1a: The
1b: quick
1c: brown
2a: fox
2b: jumped
3a: the
3c: dog
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
SortedList クラス | SortedList メンバ | System.Collections 名前空間 | Remove