📜  C#|将容量设置为SortedList对象中元素的实际数量

📅  最后修改于: 2021-05-29 20:35:14             🧑  作者: Mango

SortedList.TrimToSize方法用于将容量设置为SortedList对象中元素的实际数量。

句法:

public virtual void TrimToSize ();

异常:如果SortedList对象为只读或SortedList具有固定大小,则此方法将引发NotSupportedException

下面的程序说明了上面讨论的方法的使用:

范例1:

// C# program to illustrate the
// SortedList.TrimToSize() Method
using System;
using System.Collections;
  
class GFG {
  
    // Main method
    public static void Main()
    {
  
        // create and initalize new SortedList
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("1", "C++");
        mylist.Add("2", "Java");
        mylist.Add("3", "DSA");
        mylist.Add("4", "Python");
        mylist.Add("5", "C#");
        mylist.Add("6", "HTML");
  
        // Capacity of SortedList before trimmimg
        Console.WriteLine("Before trimming the capacity is: {0}",
                                                mylist.Capacity);
  
        // trim the SortedList
        mylist.TrimToSize();
  
        // Capacity of ArrayList after trimming
        Console.WriteLine("After trimming the capacity is: {0}",
                                               mylist.Capacity);
    }
}
输出:
Before trimming the capacity is: 16
After trimming the capacity is: 6

范例2:

// C# program to illustrate
// SortedList.TrimToSize() Method
using System;
using System.Collections;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // create and initalize new SortedList
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("h", "Hello"); 
        mylist.Add("g", "Geeks!"); 
        mylist.Add("w", "Welcome");
        mylist.Add("t", "to");
        mylist.Add("n", "Noida"); 
  
        // Displaying the count, capacity
        // and values of the SortedList.
        Console.WriteLine("Before Using TrimToSize Method:");
        Console.WriteLine();
        Console.WriteLine("Count: {0}", mylist.Count);
        Console.WriteLine("Capacity: {0}", mylist.Capacity);
        Console.Write("Values are: ");
        Display(mylist);
  
        Console.WriteLine();
  
        // Trim the SortedList.
        mylist.TrimToSize();
  
        // Displaying the count, capacity
        // and values of the SortedList.
        Console.WriteLine();
        Console.WriteLine("After Using TrimToSize Method:");
        Console.WriteLine();
        Console.WriteLine("Count: {0}", mylist.Count);
        Console.WriteLine("Capacity: {0}", mylist.Capacity);
        Console.Write("Values are: ");
        Display(mylist);
  
        // Clear the SortedList
        mylist.Clear();
  
        Console.WriteLine();
  
        // Displaying the count, capacity
        // and values of the SortedList.
        Console.WriteLine();
        Console.WriteLine("After Using Clear Method:");
        Console.WriteLine();
        Console.WriteLine("Count: {0}", mylist.Count);
        Console.WriteLine("Capacity: {0}", mylist.Capacity);
        Console.Write("Values are: ");
        Display(mylist);
  
        // again trim the SortedList
        mylist.TrimToSize();
  
        Console.WriteLine();
  
        // Displaying the count, capacity
        // and values of the SortedList.
        Console.WriteLine();
        Console.WriteLine("After Again Using TrimToSize Method:");
        Console.WriteLine();
        Console.WriteLine("Count: {0}", mylist.Count);
        Console.WriteLine("Capacity: {0}", mylist.Capacity);
        Console.Write("Values are: ");
        Display(mylist);
    }
  
    // to display the values of SortedList
    public static void Display(SortedList mylist)
    {
        // taking an IList and
        // using GetValueList method
        IList vlist = mylist.GetValueList();
  
        for (int i = 0; i < mylist.Count; i++)
            Console.Write(vlist[i] + " ");
    }
}
输出:
Before Using TrimToSize Method:

Count: 5
Capacity: 16
Values are: Geeks! Hello Noida to Welcome 

After Using TrimToSize Method:

Count: 5
Capacity: 5
Values are: Geeks! Hello Noida to Welcome 

After Using Clear Method:

Count: 0
Capacity: 5
Values are: 

After Again Using TrimToSize Method:

Count: 0
Capacity: 0
Values are:

笔记:

  • 此方法的主要用途是,如果不向集合中添加任何新元素,则可以使用它来最小化集合的内存开销。
  • 要将SortedList对象重置为其初始状态,请在调用TrimToSize之前调用Clear方法。修剪空的SortedList会将SortedList的容量设置为默认容量。
  • 此方法是O(n)运算,其中n是Count。

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.sortedlist.trimtosize?view=netframework-4.7.2