ArrayList.TrimToSize メソッド
アセンブリ: mscorlib (mscorlib.dll 内)



今後コレクションに要素が追加されないことがわかっている場合は、このメソッドを使用して、コレクションのメモリ オーバーヘッドを最小化できます。
ArrayList を初期状態にリセットするには、TrimToSize の前に、Clear を呼び出します。空の ArrayList をトリムすると、ArrayList の容量は既定値に設定されます。

ArrayList の未使用部分をなくす方法と、ArrayList の値を消去する方法を次のコード例に示します。
Imports System Imports System.Collections Imports Microsoft.VisualBasic Public Class SamplesArrayList Public Shared Sub Main() ' Creates and initializes a new ArrayList. Dim myAL As New ArrayList() myAL.Add("The") myAL.Add("quick") myAL.Add("brown") myAL.Add("fox") myAL.Add("jumped") ' Displays the count, capacity and values of the ArrayList. Console.WriteLine("Initially,") Console.WriteLine(" Count : {0}", myAL.Count) Console.WriteLine(" Capacity : {0}", myAL.Capacity) Console.Write(" Values:") PrintValues(myAL) ' Trim the ArrayList. myAL.TrimToSize() ' Displays the count, capacity and values of the ArrayList. Console.WriteLine("After TrimToSize,") Console.WriteLine(" Count : {0}", myAL.Count) Console.WriteLine(" Capacity : {0}", myAL.Capacity) Console.Write(" Values:") PrintValues(myAL) ' Clear the ArrayList. myAL.Clear() ' Displays the count, capacity and values of the ArrayList. Console.WriteLine("After Clear,") Console.WriteLine(" Count : {0}", myAL.Count) Console.WriteLine(" Capacity : {0}", myAL.Capacity) Console.Write(" Values:") PrintValues(myAL) ' Trim the ArrayList again. myAL.TrimToSize() ' Displays the count, capacity and values of the ArrayList. Console.WriteLine("After the second TrimToSize,") Console.WriteLine(" Count : {0}", myAL.Count) Console.WriteLine(" Capacity : {0}", myAL.Capacity) Console.Write(" Values:") PrintValues(myAL) End Sub Public Shared Sub PrintValues(myList As IEnumerable) Dim obj As [Object] For Each obj In myList Console.Write(" {0}", obj) Next obj Console.WriteLine() End Sub 'PrintValues End Class ' This code produces the following output. ' ' Initially, ' Count : 5 ' Capacity : 16 ' Values: The quick brown fox jumped ' After TrimToSize, ' Count : 5 ' Capacity : 5 ' Values: The quick brown fox jumped ' After Clear, ' Count : 0 ' Capacity : 5 ' Values: ' After the second TrimToSize, ' Count : 0 ' Capacity : 16 ' Values:
using System; using System.Collections; public class SamplesArrayList { public static void Main() { // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" ); myAL.Add( "jumped" ); // Displays the count, capacity and values of the ArrayList. Console.WriteLine( "Initially," ); Console.WriteLine( " Count : {0}", myAL.Count ); Console.WriteLine( " Capacity : {0}", myAL.Capacity ); Console.Write( " Values:" ); PrintValues( myAL ); // Trim the ArrayList. myAL.TrimToSize(); // Displays the count, capacity and values of the ArrayList. Console.WriteLine( "After TrimToSize," ); Console.WriteLine( " Count : {0}", myAL.Count ); Console.WriteLine( " Capacity : {0}", myAL.Capacity ); Console.Write( " Values:" ); PrintValues( myAL ); // Clear the ArrayList. myAL.Clear(); // Displays the count, capacity and values of the ArrayList. Console.WriteLine( "After Clear," ); Console.WriteLine( " Count : {0}", myAL.Count ); Console.WriteLine( " Capacity : {0}", myAL.Capacity ); Console.Write( " Values:" ); PrintValues( myAL ); // Trim the ArrayList again. myAL.TrimToSize(); // Displays the count, capacity and values of the ArrayList. Console.WriteLine( "After the second TrimToSize," ); Console.WriteLine( " Count : {0}", myAL.Count ); Console.WriteLine( " Capacity : {0}", myAL.Capacity ); Console.Write( " Values:" ); PrintValues( myAL ); } public static void PrintValues( IEnumerable myList ) { foreach ( Object obj in myList ) Console.Write( " {0}", obj ); Console.WriteLine(); } } /* This code produces the following output. Initially, Count : 5 Capacity : 16 Values: The quick brown fox jumped After TrimToSize, Count : 5 Capacity : 5 Values: The quick brown fox jumped After Clear, Count : 0 Capacity : 5 Values: After the second TrimToSize, Count : 0 Capacity : 16 Values: */
using namespace System; using namespace System::Collections; void PrintValues( IEnumerable^ myList ); int main() { // Creates and initializes a new ArrayList. ArrayList^ myAL = gcnew ArrayList; myAL->Add( "The" ); myAL->Add( "quick" ); myAL->Add( "brown" ); myAL->Add( "fox" ); myAL->Add( "jumped" ); // Displays the count, capacity and values of the ArrayList. Console::WriteLine( "Initially," ); Console::WriteLine( " Count : {0}", myAL->Count ); Console::WriteLine( " Capacity : {0}", myAL->Capacity ); Console::Write( " Values:" ); PrintValues( myAL ); // Trim the ArrayList. myAL->TrimToSize(); // Displays the count, capacity and values of the ArrayList. Console::WriteLine( "After TrimToSize," ); Console::WriteLine( " Count : {0}", myAL->Count ); Console::WriteLine( " Capacity : {0}", myAL->Capacity ); Console::Write( " Values:" ); PrintValues( myAL ); // Clear the ArrayList. myAL->Clear(); // Displays the count, capacity and values of the ArrayList. Console::WriteLine( "After Clear," ); Console::WriteLine( " Count : {0}", myAL->Count ); Console::WriteLine( " Capacity : {0}", myAL->Capacity ); Console::Write( " Values:" ); PrintValues( myAL ); // Trim the ArrayList again. myAL->TrimToSize(); // Displays the count, capacity and values of the ArrayList. Console::WriteLine( "After the second TrimToSize," ); Console::WriteLine( " Count : {0}", myAL->Count ); Console::WriteLine( " Capacity : {0}", myAL->Capacity ); Console::Write( " Values:" ); PrintValues( myAL ); } void PrintValues( IEnumerable^ myList ) { IEnumerator^ myEnum = myList->GetEnumerator(); while ( myEnum->MoveNext() ) { Object^ obj = safe_cast<Object^>(myEnum->Current); Console::Write( " {0}", obj ); } Console::WriteLine(); } /* This code produces the following output. Initially, Count : 5 Capacity : 16 Values: The quick brown fox jumped After TrimToSize, Count : 5 Capacity : 5 Values: The quick brown fox jumped After Clear, Count : 0 Capacity : 5 Values: After the second TrimToSize, Count : 0 Capacity : 16 Values: */
import System.*; import System.Collections.*; public class SamplesArrayList { public static void main(String[] args) { // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); myAL.Add("The"); myAL.Add("quick"); myAL.Add("brown"); myAL.Add("fox"); myAL.Add("jumped"); // Displays the count, capacity and values of the ArrayList. Console.WriteLine("Initially,"); Console.WriteLine(" Count : {0}", (Int32)myAL.get_Count()); Console.WriteLine(" Capacity : {0}", (Int32)myAL.get_Capacity()); Console.Write(" Values:"); PrintValues(myAL); // Trim the ArrayList. myAL.TrimToSize(); // Displays the count, capacity and values of the ArrayList. Console.WriteLine("After TrimToSize,"); Console.WriteLine(" Count : {0}", (Int32)myAL.get_Count()); Console.WriteLine(" Capacity : {0}", (Int32)myAL.get_Capacity()); Console.Write(" Values:"); PrintValues(myAL); // Clear the ArrayList. myAL.Clear(); // Displays the count, capacity and values of the ArrayList. Console.WriteLine("After Clear,"); Console.WriteLine(" Count : {0}", (Int32)myAL.get_Count()); Console.WriteLine(" Capacity : {0}", (Int32)myAL.get_Capacity()); Console.Write(" Values:"); PrintValues(myAL); // Trim the ArrayList again. myAL.TrimToSize(); // Displays the count, capacity and values of the ArrayList. Console.WriteLine("After the second TrimToSize,"); Console.WriteLine(" Count : {0}", (Int32)myAL.get_Count()); Console.WriteLine(" Capacity : {0}", (Int32)myAL.get_Capacity()); Console.Write(" Values:"); PrintValues(myAL); } //main public static void PrintValues(IEnumerable myList) { IEnumerator objMyEnum = myList.GetEnumerator(); while (objMyEnum.MoveNext()) { Object obj = objMyEnum.get_Current(); Console.Write(" {0}", obj); } Console.WriteLine(); } //PrintValues } //SamplesArrayList /* This code produces the following output. Initially, Count : 5 Capacity : 16 Values: The quick brown fox jumped After TrimToSize, Count : 5 Capacity : 5 Values: The quick brown fox jumped After Clear, Count : 0 Capacity : 5 Values: After the second TrimToSize, Count : 0 Capacity : 16 Values: */
import System; import System.Collections; // Creates and initializes a new ArrayList. var myAL : ArrayList = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" ); myAL.Add( "jumped" ); // Displays the count, capacity and values of the ArrayList. Console.WriteLine( "Initially," ); Console.WriteLine( " Count : {0}", myAL.Count ); Console.WriteLine( " Capacity : {0}", myAL.Capacity ); Console.Write( " Values:" ); PrintValues( myAL ); // Trim the ArrayList. myAL.TrimToSize(); // Displays the count, capacity and values of the ArrayList. Console.WriteLine( "After TrimToSize," ); Console.WriteLine( " Count : {0}", myAL.Count ); Console.WriteLine( " Capacity : {0}", myAL.Capacity ); Console.Write( " Values:" ); PrintValues( myAL ); // Clear the ArrayList. myAL.Clear(); // Displays the count, capacity and values of the ArrayList. Console.WriteLine( "After Clear," ); Console.WriteLine( " Count : {0}", myAL.Count ); Console.WriteLine( " Capacity : {0}", myAL.Capacity ); Console.Write( " Values:" ); PrintValues( myAL ); // Trim the ArrayList again. myAL.TrimToSize(); // Displays the count, capacity and values of the ArrayList. Console.WriteLine( "After the second TrimToSize," ); Console.WriteLine( " Count : {0}", myAL.Count ); Console.WriteLine( " Capacity : {0}", myAL.Capacity ); Console.Write( " Values:" ); PrintValues( myAL ); function PrintValues( myList : IEnumerable) { var myEnumerator : System.Collections.IEnumerator = myList.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "\t{0}", myEnumerator.Current ); Console.WriteLine(); } /* This code produces the following output. Initially, Count : 5 Capacity : 16 Values: The quick brown fox jumped After TrimToSize, Count : 5 Capacity : 5 Values: The quick brown fox jumped After Clear, Count : 0 Capacity : 5 Values: After the second TrimToSize, Count : 0 Capacity : 16 Values: */

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に収録されているすべての辞書からArrayList.TrimToSize メソッドを検索する場合は、下記のリンクをクリックしてください。

- ArrayList.TrimToSize メソッドのページへのリンク