SortedList.TrimToSize メソッド
容量を SortedList 内にある実際の要素数に設定します。
Public Overridable Sub TrimToSize()
[C#]
public virtual void TrimToSize();
[C++]
public: virtual void TrimToSize();
[JScript]
public function TrimToSize();
例外
例外の種類 | 条件 |
---|---|
NotSupportedException | SortedList が読み取り専用です。
または SortedList が固定サイズです。 |
解説
今後リストに要素が追加されないことがわかっている場合は、このメソッドを使用して、リストのメモリ オーバーヘッドを最小化できます。
リスト内のすべての要素を完全に消去するには、 TrimToSize を呼び出す前に、 Clear メソッドを呼び出します。空の SortedList に対して TrimToSize メソッドを使用すると、 SortedList の容量は 0 ではなく、既定値に設定されます。
使用例
[Visual Basic, C#, C++] SortedList の未使用部分をなくす方法と、 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("one", "The")
mySL.Add("two", "quick")
mySL.Add("three", "brown")
mySL.Add("four", "fox")
mySL.Add("five", "jumped")
' Displays the count, capacity and values of the SortedList.
Console.WriteLine("Initially,")
Console.WriteLine(" Count : {0}", mySL.Count)
Console.WriteLine(" Capacity : {0}", mySL.Capacity)
Console.WriteLine(" Values:")
PrintKeysAndValues(mySL)
' Trims the SortedList.
mySL.TrimToSize()
' Displays the count, capacity and values of the SortedList.
Console.WriteLine("After TrimToSize,")
Console.WriteLine(" Count : {0}", mySL.Count)
Console.WriteLine(" Capacity : {0}", mySL.Capacity)
Console.WriteLine(" Values:")
PrintKeysAndValues(mySL)
' Clears the SortedList.
mySL.Clear()
' Displays the count, capacity and values of the SortedList.
Console.WriteLine("After Clear,")
Console.WriteLine(" Count : {0}", mySL.Count)
Console.WriteLine(" Capacity : {0}", mySL.Capacity)
Console.WriteLine(" Values:")
PrintKeysAndValues(mySL)
' Trims the SortedList again.
mySL.TrimToSize()
' Displays the count, capacity and values of the SortedList.
Console.WriteLine("After the second TrimToSize,")
Console.WriteLine(" Count : {0}", mySL.Count)
Console.WriteLine(" Capacity : {0}", mySL.Capacity)
Console.WriteLine(" Values:")
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.
'
' Initially,
' Count : 5
' Capacity : 16
' Values:
' -KEY- -VALUE-
' five: jumped
' four: fox
' one: The
' three: brown
' two: quick
'
' After TrimToSize,
' Count : 5
' Capacity : 5
' Values:
' -KEY- -VALUE-
' five: jumped
' four: fox
' one: The
' three: brown
' two: quick
'
' After Clear,
' Count : 0
' Capacity : 16
' Values:
' -KEY- -VALUE-
'
'
' After the second TrimToSize,
' Count : 0
' Capacity : 16
' Values:
' -KEY- -VALUE-
[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( "one", "The" );
mySL.Add( "two", "quick" );
mySL.Add( "three", "brown" );
mySL.Add( "four", "fox" );
mySL.Add( "five", "jumped" );
// Displays the count, capacity and values of the SortedList.
Console.WriteLine( "Initially," );
Console.WriteLine( " Count : {0}", mySL.Count );
Console.WriteLine( " Capacity : {0}", mySL.Capacity );
Console.WriteLine( " Values:" );
PrintKeysAndValues( mySL );
// Trims the SortedList.
mySL.TrimToSize();
// Displays the count, capacity and values of the SortedList.
Console.WriteLine( "After TrimToSize," );
Console.WriteLine( " Count : {0}", mySL.Count );
Console.WriteLine( " Capacity : {0}", mySL.Capacity );
Console.WriteLine( " Values:" );
PrintKeysAndValues( mySL );
// Clears the SortedList.
mySL.Clear();
// Displays the count, capacity and values of the SortedList.
Console.WriteLine( "After Clear," );
Console.WriteLine( " Count : {0}", mySL.Count );
Console.WriteLine( " Capacity : {0}", mySL.Capacity );
Console.WriteLine( " Values:" );
PrintKeysAndValues( mySL );
// Trims the SortedList again.
mySL.TrimToSize();
// Displays the count, capacity and values of the SortedList.
Console.WriteLine( "After the second TrimToSize," );
Console.WriteLine( " Count : {0}", mySL.Count );
Console.WriteLine( " Capacity : {0}", mySL.Capacity );
Console.WriteLine( " Values:" );
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.
Initially,
Count : 5
Capacity : 16
Values:
-KEY- -VALUE-
five: jumped
four: fox
one: The
three: brown
two: quick
After TrimToSize,
Count : 5
Capacity : 5
Values:
-KEY- -VALUE-
five: jumped
four: fox
one: The
three: brown
two: quick
After Clear,
Count : 0
Capacity : 16
Values:
-KEY- -VALUE-
After the second TrimToSize,
Count : 0
Capacity : 16
Values:
-KEY- -VALUE-
*/
[C++]
#using <mscorlib.dll>
#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 __gc *mySL = new SortedList();
mySL->Add( S"one", S"The" );
mySL->Add( S"two", S"quick" );
mySL->Add( S"three", S"brown" );
mySL->Add( S"four", S"fox" );
mySL->Add( S"five", S"jumped" );
// Displays the count, capacity and values of the SortedList.
Console::WriteLine( "Initially," );
Console::WriteLine( " Count : {0}", __box(mySL->Count) );
Console::WriteLine( " Capacity : {0}", __box(mySL->Capacity) );
Console::WriteLine( " Values:" );
PrintKeysAndValues( mySL );
// Trims the SortedList.
mySL->TrimToSize();
// Displays the count, capacity and values of the SortedList.
Console::WriteLine( "After TrimToSize," );
Console::WriteLine( " Count : {0}", __box(mySL->Count) );
Console::WriteLine( " Capacity : {0}", __box(mySL->Capacity) );
Console::WriteLine( " Values:" );
PrintKeysAndValues( mySL );
// Clears the SortedList.
mySL->Clear();
// Displays the count, capacity and values of the SortedList.
Console::WriteLine( "After Clear," );
Console::WriteLine( " Count : {0}", __box(mySL->Count) );
Console::WriteLine( " Capacity : {0}", __box(mySL->Capacity) );
Console::WriteLine( " Values:" );
PrintKeysAndValues( mySL );
// Trims the SortedList again.
mySL->TrimToSize();
// Displays the count, capacity and values of the SortedList.
Console::WriteLine( "After the second TrimToSize," );
Console::WriteLine( " Count : {0}", __box(mySL->Count) );
Console::WriteLine( " Capacity : {0}", __box(mySL->Capacity) );
Console::WriteLine( " Values:" );
PrintKeysAndValues( mySL );
}
/*
This code produces the following output.
Initially,
Count : 5
Capacity : 16
Values:
-KEY- -VALUE-
five: jumped
four: fox
one: The
three: brown
two: quick
After TrimToSize,
Count : 5
Capacity : 5
Values:
-KEY- -VALUE-
five: jumped
four: fox
one: The
three: brown
two: quick
After Clear,
Count : 0
Capacity : 16
Values:
-KEY- -VALUE-
After the second TrimToSize,
Count : 0
Capacity : 16
Values:
-KEY- -VALUE-
*/
[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 名前空間 | Clear | Capacity | Count