SortedList.TrimToSize メソッド
アセンブリ: mscorlib (mscorlib.dll 内)
構文
例外
解説
今後リストに要素が追加されないことがわかっている場合は、このメソッドを使用して、リストのメモリ オーバーヘッドを最小化できます。
SortedList を初期状態にリセットするには、TrimToSize の前に、Clear を呼び出します。空の SortedList をトリムすると、SortedList の容量は既定値に設定されます。
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-
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- */
#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( "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 ); } /* 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- */
import System.*; import System.Collections.*; public class SamplesSortedList { public static void main(String[] args) { // 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}", System.Convert.ToString(mySL.get_Count())); Console.WriteLine(" Capacity : {0}", System.Convert.ToString(mySL.get_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}", System.Convert.ToString(mySL.get_Count())); Console.WriteLine(" Capacity : {0}", System.Convert.ToString(mySL.get_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}", System.Convert.ToString(mySL.get_Count())); Console.WriteLine(" Capacity : {0}", System.Convert.ToString(mySL.get_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}", System.Convert.ToString(mySL.get_Count())); Console.WriteLine(" Capacity : {0}", System.Convert.ToString(mySL.get_Capacity())); Console.WriteLine(" Values:"); PrintKeysAndValues(mySL); } //main public static void PrintKeysAndValues(SortedList myList) { Console.WriteLine("\t-KEY-\t-VALUE-"); for (int i = 0; i < myList.get_Count(); i++) { Console.WriteLine("\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i)); } Console.WriteLine(); } //PrintKeysAndValues } //SamplesSortedList /* 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- */
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Weblioに収録されているすべての辞書からSortedList.TrimToSize メソッドを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からSortedList.TrimToSize メソッドを検索
- SortedList.TrimToSize メソッドのページへのリンク