C# 中 ArrayList 類的 Capacity 屬性是什麼?


ArrayList 類的 capacity 屬性獲取或設定 ArrayList 可包含的元素數量。

容量始終大於計數。對於容量屬性−

arrList.Capacity

預設容量為 4。如果有 5 個元素,則其容量加倍並變為 8。以此類推。

你可以嘗試執行以下程式碼,以便在 C# 中實現 Capacity 屬性。它還展示了我們在上面討論的內容 −

示例

 即時演示

using System;
using System.Collections;

class Demo {
   public static void Main() {
      ArrayList arrList = new ArrayList();
      arrList.Add(19);
      arrList.Add(44);
      arrList.Add(22);

      ArrayList arrList2 = new ArrayList();
      arrList2.Add(19);
      arrList2.Add(44);
      arrList2.Add(64);
      arrList2.Add(32);
      arrList2.Add(99);

      Console.WriteLine("ArrayList1 - Total elements: "+arrList.Count);
      Console.WriteLine("ArrayList1 - Capacity: "+arrList.Capacity);

      Console.WriteLine("ArrayList2 - Total elements: "+arrList2.Count);
      Console.WriteLine("ArrayList2 - Capacity: "+arrList2.Capacity);
     
   }
}

輸出

ArrayList1 - Total elements: 3
ArrayList1 - Capacity: 4
ArrayList2 - Total elements: 5
ArrayList2 - Capacity: 8

更新於: 20-Jun-2020

3K+ 瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.