ArrayList.Capacity 屬性

定義

取得或設定 ArrayList 可包含的項目數目。Gets or sets the number of elements that the ArrayList can contain.

public:
 virtual property int Capacity { int get(); void set(int value); };
public virtual int Capacity { get; set; }
member this.Capacity : int with get, set
Public Overridable Property Capacity As Integer

屬性值

ArrayList 可包含的項目數。The number of elements that the ArrayList can contain.

例外狀況

Capacity 是設定為小於 Count 的值。Capacity is set to a value that is less than Count.

系統上沒有足夠的記憶體可用。There is not enough memory available on the system.

備註

CapacityArrayList 可以儲存的元素數目。Capacity is the number of elements that the ArrayList can store. Count 是實際在 ArrayList中的元素數目。Count is the number of elements that are actually in the ArrayList.

Capacity 一律大於或等於 CountCapacity is always greater than or equal to Count. 如果 Count 在加入元素時超過 Capacity,則在複製舊的專案並新增新專案之前,會重新配置內部陣列,以自動增加容量。If Count exceeds Capacity while adding elements, the capacity is automatically increased by reallocating the internal array before copying the old elements and adding the new elements.

您可以藉由呼叫 TrimToSize 或明確設定 Capacity 屬性來降低容量。The capacity can be decreased by calling TrimToSize or by setting the Capacity property explicitly. 明確設定 Capacity 的值時,內部陣列也會重新配置以容納指定的容量。When the value of Capacity is set explicitly, the internal array is also reallocated to accommodate the specified capacity.

抓取這個屬性的值是 O(1) 作業;將屬性設定為 O(n) 作業,其中 n 是新的容量。Retrieving the value of this property is an O(1) operation; setting the property is an O(n) operation, where n is the new capacity.

適用於

另請參閱