Mega Code Archive

 
Categories / C# / Collections Data Structure
 

ArrayList Demo

/* Learning C#  by Jesse Liberty Publisher: O'Reilly  ISBN: 0596003765 */  using System;  using System.Collections;  namespace ArrayListDemo  {      // a class to hold in the array list      class Employee      {          private int empID;          public Employee(int empID)          {              this.empID = empID;          }          public override  string ToString()          {              return empID.ToString();          }          public int EmpID          {              get { return empID; }              set { empID = value; }          }      }      public class ArrayListDemoTester      {          public void Run()          {              ArrayList empArray = new ArrayList();              ArrayList intArray = new ArrayList();              // populate the arraylists              for (int i = 0;i<5;i++)              {                  empArray.Add(new Employee(i+100));                  intArray.Add(i*5);              }              // print each member of the array              foreach (int i in intArray)              {                  Console.Write("{0} ", i.ToString());              }              Console.WriteLine("\n");              // print each employee              foreach(Employee e in empArray)              {                  Console.Write("{0} ", e.ToString());              }              Console.WriteLine("\n");              Console.WriteLine("empArray.Capacity: {0}",                  empArray.Capacity);          }          [STAThread]          static void Main()          {              ArrayListDemoTester t = new ArrayListDemoTester();              t.Run();          }      }  }