ArrayList.Capacity : ArrayList « System.Collections « C# / C Sharp by API






ArrayList.Capacity

  

using System; 
using System.Collections; 
 
public class ArrayListDemo { 
  public static void Main() { 
    ArrayList al = new ArrayList(); 
     
    Console.WriteLine("Initial capacity: " + al.Capacity); 
    Console.WriteLine("Initial number of elements: " + al.Count); 
 
    Console.WriteLine(); 
 
    Console.WriteLine("Adding 6 elements"); 
    // Add elements to the array list 
    al.Add('C'); 
    al.Add('A'); 
    al.Add('E'); 
    al.Add('B'); 
    al.Add('D'); 
    al.Add('F'); 
 
    Console.WriteLine("Current capacity: " + 
                       al.Capacity); 
    Console.WriteLine("Number of elements: " + 
                       al.Count); 
 
    // Display the array list using array indexing. 
    Console.Write("Current contents: "); 
    for(int i=0; i < al.Count; i++) 
      Console.Write(al[i] + " "); 
    Console.WriteLine("\n"); 
 
    Console.WriteLine("Removing 2 elements"); 
    // Remove elements from the array list. 
    al.Remove('F'); 
    al.Remove('A'); 
 
    Console.WriteLine("Current capacity: " + 
                       al.Capacity); 
    Console.WriteLine("Number of elements: " + 
                       al.Count); 
 
    // Use foreach loop to display the list. 
    Console.Write("Contents: "); 
    foreach(char c in al) 
      Console.Write(c + " "); 
    Console.WriteLine("\n"); 
 
    Console.WriteLine("Adding 20 more elements"); 
    // Add enough elements to force al to grow. 
    for(int i=0; i < 20; i++) 
      al.Add((char)('a' + i)); 
    Console.WriteLine("Current capacity: " + 
                       al.Capacity); 
    Console.WriteLine("Number of elements after adding 20: " + 
                       al.Count); 
    Console.Write("Contents: "); 
    foreach(char c in al) 
      Console.Write(c + " "); 
    Console.WriteLine("\n"); 
 
    // Change contents using array indexing. 
    Console.WriteLine("Change first three elements"); 
    al[0] = 'X'; 
    al[1] = 'Y'; 
    al[2] = 'Z'; 
    Console.Write("Contents: "); 
    foreach(char c in al) 
      Console.Write(c + " "); 
    Console.WriteLine(); 
  } 
}

   
    
  








Related examples in the same category

1.new ArrayList()
2.new ArrayList(Collection c)
3.ArrayList.Add
4.ArrayList.AddRange
5.ArrayList.BinarySearch
6.ArrayList.Clear()
7.ArrayList.Clone
8.ArrayList.Contains
9.ArrayList.CopyTo
10.ArrayList.GetEnumerator()
11.ArrayList.GetRange
12.ArrayList.IndexOf
13.ArrayList.Insert
14.ArrayList.InsertRange()
15.ArrayList.IsFixedSize
16.ArrayList.IsReadOnly
17.ArrayList.LastIndexOf
18.ArrayList.Remove
19.ArrayList.RemoveAt
20.ArrayList.RemoveRange
21.ArrayList.Reverse()
22.ArrayList.SetRange
23.ArrayList.Sort()
24.ArrayList.Synchronized
25.ArrayList.ToArray
26.ArrayList.ToArray(typeof(T))
27.ArrayList.TrimToSize()