SortedList.TrimToSize 方法

定义

将容量设置为 SortedList 对象中元素的实际数目。

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

例外

SortedList 对象是只读的。

- 或 -

SortedList 具有固定的大小。

示例

下面的代码示例演示如何剪裁 对象的未使用部分 SortedList 以及如何清除其值。

#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-

注解

如果未向集合添加新元素,则此方法可用于最大程度地减少集合的内存开销。

若要将 SortedList 对象重置为其初始状态,请在 Clear 调用 之前调用 TrimToSize方法。 剪裁空 SortedList 会将 的 SortedList 容量设置为默认容量。

此方法是一个 O(n) 操作,其中 nCount

适用于

另请参阅