ArrayList.TrimToSize メソッド
容量を ArrayList 内にある実際の要素数に設定します。
Public Overridable Sub TrimToSize()
[C#]
public virtual void TrimToSize();
[C++]
public: virtual void TrimToSize();
[JScript]
public function TrimToSize();
例外
例外の種類 | 条件 |
---|---|
NotSupportedException | ArrayList が読み取り専用です。
または ArrayList が固定サイズです。 |
解説
今後リストに要素が追加されないことがわかっている場合は、このメソッドを使用して、リストのメモリ オーバーヘッドを最小化できます。
リスト内のすべての要素を完全に消去するには、 TrimToSize を呼び出す前に、 Clear メソッドを呼び出します。空の ArrayList に対して TrimToSize メソッドを使用すると、 ArrayList の容量は 0 ではなく、既定値に設定されます。
使用例
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 myEnumerator As System.Collections.IEnumerator = _
myList.GetEnumerator()
While myEnumerator.MoveNext()
Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
End While
Console.WriteLine()
End Sub
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:
[C#]
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 ) {
System.Collections.IEnumerator myEnumerator = 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:
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable* myList );
void main() {
// Creates and initializes a new ArrayList.
ArrayList* myAL = new ArrayList();
myAL->Add( S"The" );
myAL->Add( S"quick" );
myAL->Add( S"brown" );
myAL->Add( S"fox" );
myAL->Add( S"jumped" );
// Displays the count, capacity and values of the ArrayList.
Console::WriteLine( "Initially," );
Console::WriteLine( " Count : {0}", __box(myAL->Count) );
Console::WriteLine( " Capacity : {0}", __box(myAL->Capacity) );
Console::Write( " Values:" );
PrintValues( myAL );
// Trims the ArrayList.
myAL->TrimToSize();
// Displays the count, capacity and values of the ArrayList.
Console::WriteLine( "After TrimToSize," );
Console::WriteLine( " Count : {0}", __box(myAL->Count) );
Console::WriteLine( " Capacity : {0}", __box(myAL->Capacity) );
Console::Write( " Values:" );
PrintValues( myAL );
// Clears the ArrayList.
myAL->Clear();
// Displays the count, capacity and values of the ArrayList.
Console::WriteLine( "After Clear," );
Console::WriteLine( " Count : {0}", __box(myAL->Count) );
Console::WriteLine( " Capacity : {0}", __box(myAL->Capacity) );
Console::Write( " Values:" );
PrintValues( myAL );
// Trims the ArrayList again.
myAL->TrimToSize();
// Displays the count, capacity and values of the ArrayList.
Console::WriteLine( "After the second TrimToSize," );
Console::WriteLine( " Count : {0}", __box(myAL->Count) );
Console::WriteLine( " Capacity : {0}", __box(myAL->Capacity) );
Console::Write( " Values:" );
PrintValues( myAL );
}
void PrintValues( IEnumerable* myList ) {
System::Collections::IEnumerator* myEnumerator = 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:
*/
[JScript]
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 NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
ArrayList クラス | ArrayList メンバ | System.Collections 名前空間 | Clear | Capacity | Count