Create ArrayList

In this chapter you will learn:

  1. How to create ArrayList

ArrayList creation

The following code demonstrates the 3 constructors of ArrayList.

using System;/*  ja va 2  s  .  c  o  m*/
using System.Collections;

class Class1
{
  [STAThread]
  static void Main(string[] args)
  {
           ArrayList al1 = new ArrayList();
           ArrayList al2 = new ArrayList( 10 );

           int[] intArr = new int[10];
           ArrayList al3 = new ArrayList( intArr );

           ArrayList myArrayList = new ArrayList();
           myArrayList.Capacity = 10;

           intArr = {2, 3, 4, 5};
           myArrayList.Add( 1 );
           myArrayList.AddRange( intArr );
           Console.WriteLine( myArrayList.Capacity );

           myArrayList.Remove( 1 );
           myArrayList.RemoveRange( 1, 2 );

           myArrayList.Insert( 1, 3 );
           myArrayList.InsertRange( 0, intArr );

           foreach( object elem in myArrayList )
           {
               Console.WriteLine( elem );
           }
       }
}

Next chapter...

What you will learn in the next chapter:

  1. How to get elements in an ArrayList
  2. Capacity and element count
  3. How to release memory down to the element size