ArrayList.TrimToSize メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
容量を ArrayList 内にある実際の要素数に設定します。
public:
virtual void TrimToSize();
public virtual void TrimToSize ();
abstract member TrimToSize : unit -> unit
override this.TrimToSize : unit -> unit
Public Overridable Sub TrimToSize ()
例外
例
次のコード例は、 の未使用部分をトリミングする方法と、 の ArrayList 値をクリアする方法を ArrayList示しています。
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( "jumps" );
// 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 jumps
After TrimToSize,
Count : 5
Capacity : 5
Values: The quick brown fox jumps
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( "jumps" );
// 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 jumps
After TrimToSize,
Count : 5
Capacity : 5
Values: The quick brown fox jumps
After Clear,
Count : 0
Capacity : 5
Values:
After the second TrimToSize,
Count : 0
Capacity : 16
Values:
*/
Imports System.Collections
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("jumps")
' 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
End Class
' This code produces the following output.
'
' Initially,
' Count : 5
' Capacity : 16
' Values: The quick brown fox jumps
' After TrimToSize,
' Count : 5
' Capacity : 5
' Values: The quick brown fox jumps
' After Clear,
' Count : 0
' Capacity : 5
' Values:
' After the second TrimToSize,
' Count : 0
' Capacity : 16
' Values:
注釈
このメソッドを使用すると、新しい要素がコレクションに追加されない場合に、コレクションのメモリ オーバーヘッドを最小限に抑えることができます。
を初期状態にリセット ArrayList するには、 を呼び出す前に メソッドを Clear 呼び出します TrimToSize。 空 ArrayList をトリミングすると、 の容量が ArrayList 既定の容量に設定されます。
このメソッドは 操作です O(n)
。ここで n
、 は Countです。
適用対象
こちらもご覧ください
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET