Assembly : mscorlib (dans mscorlib.dll)
SyntaxePublic Overridable Sub TrimToSize
Dim instance As ArrayList instance.TrimToSize
public virtual void TrimToSize ()
public: virtual void TrimToSize ()
public void TrimToSize ()
public function TrimToSize ()
Exceptions| Type d'exception | Condition |
|---|---|
| ArrayList est en lecture seule. - ou - ArrayList est de taille fixe. |
NotesCette méthode peut être utilisée pour réduire au minimum la mémoire occupée par une collection si aucun nouvel élément ne doit être ajouté à celle-ci.
Pour rétablir l'état initial de ArrayList, appelez la méthode Clear avant d'appeler TrimToSize. Si la fonction Trim est appliquée à un ArrayList vide, la capacité de ArrayList prend sa valeur par défaut.
Cette méthode est une opération O(n), où n est égal à Count.
ExempleL'exemple de code suivant montre comment supprimer les parties inutilisées de ArrayList et comment effacer les valeurs de 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: */
Plates-formesWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Informations de version
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ