Partager via


SortedList.TrimToSize Méthode

Définition

Affecte à la capacité le nombre réel d'éléments d'un objet SortedList.

public:
 virtual void TrimToSize();
public virtual void TrimToSize ();
abstract member TrimToSize : unit -> unit
override this.TrimToSize : unit -> unit
Public Overridable Sub TrimToSize ()

Exceptions

L'objet SortedList est en lecture seule.

- ou -

SortedList est de taille fixe.

Exemples

L’exemple de code suivant montre comment découper les parties inutilisées d’un SortedList objet et comment effacer ses valeurs.

#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", "jumps" );
   
   // 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:   jumps
        four:   fox
        one:    The
        three:  brown
        two:    quick

After TrimToSize,
   Count    : 5
   Capacity : 5
   Values:
        -KEY-   -VALUE-
        five:   jumps
        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", "jumps" );

      // 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:    jumps
    four:    fox
    one:    The
    three:    brown
    two:    quick

After TrimToSize,
   Count    : 5
   Capacity : 5
   Values:
    -KEY-    -VALUE-
    five:    jumps
    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-
*/
Imports System.Collections

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", "jumps")
        
        ' 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:    jumps
'     four:    fox
'     one:    The
'     three:    brown
'     two:    quick
'
' After TrimToSize,
'    Count    : 5
'    Capacity : 5
'    Values:
'     -KEY-    -VALUE-
'     five:    jumps
'     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-

Remarques

Cette méthode peut être utilisée pour réduire la surcharge de mémoire d’une collection si aucun nouvel élément n’est ajouté à la collection.

Pour rétablir l’état initial d’un SortedList objet, appelez la Clear méthode avant d’appeler TrimToSize. La suppression d’un vide SortedList définit la capacité du sur SortedList la capacité par défaut.

Cette méthode est une O(n) opération, où n est Count.

S’applique à

Voir aussi