📜  C#| SortedList的容量

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

SortedList.Capacity属性用于获取或设置SortedList对象的容量。

句法:

public virtual int Capacity { get; set; }

返回值:该属性返回SortedList对象可以包含的System.Int32类型的元素数。

例外情况:

  • ArgumentOutOfRangeException:如果分配的值小于SortedList对象中的当前元素数。
  • OutOfMemoryException:如果系统上没有足够的可用内存。

容量与数量:

  • 计数始终小于容量。添加元素时,如果Count超过Capacity,则在复制旧元素并添加新元素之前,通过重新分配内部数组,Capacity将自动增加。
  • 容量是在调整所需列表大小之前列表可以存储的元素数。但是Count是列表中实际存在的元素数。
  • 如果“容量”远大于“计数”,则用户可以通过调用TrimExcess方法或将“容量”显式设置为较低的值来减少容量。
  • 如果明确地确定了Capacity,则内部数组也将重新分配以容纳指定的容量,并复制所有元素。
  • 检索Capacity属性的值是O(1)操作,而将Capacity设置为O(n)操作,其中n是新容量。

下面给出了一些示例,以更好地理解实现:

范例1:

// C# program to illustrate the
// Capacity Property of SortedList
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        // Here we are not setting
        // Capacity explicitly
        SortedList mySortedList = new SortedList();
  
        // Adding elements to SortedList
        mySortedList.Add("1", "C#");
        mySortedList.Add("2", "Java");
        mySortedList.Add("3", "DSA");
        mySortedList.Add("4", "Python");
        mySortedList.Add("5", "C");
  
        // Printing the Capacity of mySortedList
        Console.WriteLine("Capacity Is: " + mySortedList.Capacity);
  
        // Printing the Count of mySortedList
        Console.WriteLine("Count Is: " + mySortedList.Count);
  
        // Adding some more
        // elements in mySortedList
        mySortedList.Add("6", "C++");
        mySortedList.Add("7", "HTML");
        mySortedList.Add("8", "CSS");
        mySortedList.Add("9", "Web");
        mySortedList.Add("10", "DBMS");
        mySortedList.Add("11", "CN");
        mySortedList.Add("12", "Ruby");
        mySortedList.Add("13", "Perl");
        mySortedList.Add("14", "R");
        mySortedList.Add("15", "GO");
        mySortedList.Add("16", "Scala");
        mySortedList.Add("17", "Big Data");
  
        // Printing the Capacity of mySortedList
        // It will give output 32 as internally
        // SortedList is resized
        Console.WriteLine("Capacity Is: " + mySortedList.Capacity);
  
        // Printing the Count of mySortedList
        Console.WriteLine("Count Is: " + mySortedList.Count);
    }
}

输出:

Capacity Is: 16
Count Is: 5
Capacity Is: 32
Count Is: 17

范例2:

// C# program to illustrate the
// Capacity Property of SortedList
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        // Here we are setting Capacity
        // explicitly i.e. 7
        SortedList mySortedList = new SortedList(7);
  
        // Printing the Capacity of mySortedList
        Console.WriteLine("Capacity Is: " + mySortedList.Capacity);
  
        // Printing the Count of mySortedList
        Console.WriteLine("Count Is: " + mySortedList.Count);
  
        // Adding elements to SortedList
        mySortedList.Add("1", "C#");
        mySortedList.Add("2", "Java");
        mySortedList.Add("3", "DSA");
        mySortedList.Add("4", "Python");
        mySortedList.Add("5", "C");
  
        // Printing the Capacity of mySortedList
        Console.WriteLine("Capacity Is: " + mySortedList.Capacity);
  
        // Printing the Count of mySortedList
        Console.WriteLine("Count Is: " + mySortedList.Count);
  
        // Adding some more
        // elements in firstlist
        mySortedList.Add("6", "C++");
        mySortedList.Add("7", "HTML");
        mySortedList.Add("8", "CSS");
        mySortedList.Add("9", "Web");
  
        // Printing the Capacity of mySortedList
        // It will give output 14 as internally
        // SortedList is resized
        Console.WriteLine("Capacity Is: " + mySortedList.Capacity);
  
        // Printing the Count of firstlist
        Console.WriteLine("Count Is: " + mySortedList.Count);
    }
}

输出:

Capacity Is: 7
Count Is: 0
Capacity Is: 7
Count Is: 5
Capacity Is: 14
Count Is: 9

参考:

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