Set the capacity to the actual number of elements in the ArrayList in C#?

CsharpServer Side ProgrammingProgramming

To set the capacity to the actual number of elements in the ArrayList, the code is as follows −

Example

 Live Demo

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args) {
      ArrayList list1 = new ArrayList();
      list1.Add("A");
      list1.Add("B");
      list1.Add("C");
      list1.Add("D");
      list1.Add("E");
      list1.Add("F");
      list1.Add("G");
      list1.Add("H");
      list1.Add("I");
      Console.WriteLine("Elements in ArrayList1...");
      foreach (string res in list1) {
         Console.WriteLine(res);
      }
      ArrayList list2 = new ArrayList();
      list2.Add("A");
      list2.Add("B");
      list2.Add("C");
      list2.Add("D");
      list2.Add("E");
      list2.Add("F");
      list2.Add("G");
      list2.Add("H");
      list2.Add("I");
      Console.WriteLine("Elements in ArrayList2...");
      foreach (string res in list2) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Count of elements in ArrayList2 = " + list2.Count);
      list2.RemoveAt(5);
      Console.WriteLine("Count of elements in ArrayList2 (Updated) = " + list2.Count);
      Console.WriteLine("Enumerator iterating the ArrayList2...");
      IEnumerator demoEnum = list2.GetEnumerator();
      while (demoEnum.MoveNext()) {
         Console.WriteLine(demoEnum.Current);
      }
      Console.WriteLine("Capacity of ArrayList2 = " + list2.Capacity);
      list2.TrimToSize();
      Console.WriteLine("Capacity of ArrayList2 (updated) = " + list2.Capacity);
   }
}

Output

This will produce the following output −

Elements in ArrayList1...
A
B
C
D
E
F
G
H
I
Elements in ArrayList2...
A
B
C
D
E
F
G
H
I
Count of elements in ArrayList2 = 9
Count of elements in ArrayList2 (Updated) = 8
Enumerator iterating the ArrayList2...
A
B
C
D
E
G
H
I
Capacity of ArrayList2 = 16
Capacity of ArrayList2 (updated) = 8

Example

Let us now see another example −

 Live Demo

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      ArrayList list = new ArrayList();
      list.Add("One");
      list.Add("Two");
      list.Add("Three");
      list.Add("Four");
      list.Add("Five");
      list.Add("Six");
      list.Add("Seven");
      list.Add("Eight");
      Console.WriteLine("ArrayList elements...");  
      foreach(string str in list) {
         Console.WriteLine(str);
      }
      Console.WriteLine("ArrayList is read-only? = "+list.IsReadOnly);
      Console.WriteLine("Does the element Six in the ArrayList? = "+list.Contains("Six"));
      list.Insert(4, "Twelve");
      Console.WriteLine("ArrayList elements...UPDATED");
      foreach(string str in list) {
         Console.WriteLine(str);
      }
      Console.WriteLine("Capacity of ArrayList = " + list.Capacity);
      list.TrimToSize();
      Console.WriteLine("Capacity of ArrayList (updated) = " + list.Capacity);
   }
}

Output

This will produce the following output −

ArrayList elements...
One
Two
Three
Four
Five
Six
Seven
Eight
ArrayList is read-only? = False
Does the element Six in the ArrayList? = True
ArrayList elements...UPDATED
One
Two
Three
Four
Twelve
Five
Six
Seven
Eight
Capacity of ArrayList = 16
Capacity of ArrayList (updated) = 9
raja
Published on 16-Dec-2019 10:16:59
Advertisements