SortedList.TrimToSize 方法

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

**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overridable Sub TrimToSize
用法
Dim instance As SortedList

instance.TrimToSize
public virtual void TrimToSize ()
public:
virtual void TrimToSize ()
public void TrimToSize ()
public function TrimToSize ()

异常

异常类型 条件

NotSupportedException

SortedList 为只读。

- 或 -

SortedList 具有固定大小。

备注

如果不向集合中添加新元素,则此方法可用于最小化集合的内存开销。

若要将 SortedList 重置为它的初始状态,请在调用 TrimToSize 之前调用 Clear 方法。截去空 SortedList 会将 SortedList 的容量设置为默认容量。

此方法的运算复杂度为 O(n),其中 n 是 Count

示例

下面的示例说明如何截去 SortedList 的未使用部分以及如何清除 SortedList 的值。

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

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

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

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

After TrimToSize,
   Count    : 5
   Capacity : 5
   Values:
        -KEY-   -VALUE-
        five:   jumped
        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-

*/
import System.*;
import System.Collections.*;

public class SamplesSortedList
{
    public static void main(String[] args)
    {
        // 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", "jumped");

        // Displays the count, capacity and values of the SortedList.
        Console.WriteLine("Initially,");
        Console.WriteLine("   Count    : {0}", 
            System.Convert.ToString(mySL.get_Count()));
        Console.WriteLine("   Capacity : {0}", 
            System.Convert.ToString(mySL.get_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}", 
            System.Convert.ToString(mySL.get_Count()));
        Console.WriteLine("   Capacity : {0}", 
            System.Convert.ToString(mySL.get_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}", 
            System.Convert.ToString(mySL.get_Count()));
        Console.WriteLine("   Capacity : {0}", 
            System.Convert.ToString(mySL.get_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}", 
            System.Convert.ToString(mySL.get_Count()));
        Console.WriteLine("   Capacity : {0}", 
            System.Convert.ToString(mySL.get_Capacity()));
        Console.WriteLine("   Values:");
        PrintKeysAndValues(mySL);
    } //main

    public static void PrintKeysAndValues(SortedList myList)
    {
        Console.WriteLine("\t-KEY-\t-VALUE-");
        for (int i = 0; i < myList.get_Count(); i++) {
            Console.WriteLine("\t{0}:\t{1}", myList.GetKey(i), 
                myList.GetByIndex(i));
        }
        Console.WriteLine();
    } //PrintKeysAndValues

} //SamplesSortedList

/* 
 This code produces the following output.
 
 Initially,
    Count    : 5
    Capacity : 16
    Values:
     -KEY-    -VALUE-
     five:    jumped
     four:    fox
     one:    The
     three:    brown
     two:    quick
 
 After TrimToSize,
    Count    : 5
    Capacity : 5
    Values:
     -KEY-    -VALUE-
     five:    jumped
     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-
 */

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

SortedList 类
SortedList 成员
System.Collections 命名空间
Clear
Capacity
Count