Monday, August 2, 2010

Program to explain ArrayList.


Write a program to explain ArrayList.
using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace Csharp
{
    class collections
    {
        public static void Main(string[] args)
        {
            arraylist list = new arraylist();
            list.list();
            list.list2();
            Console.ReadLine();
        }
    }
    class arraylist
    {
        public void list()
        {
            ArrayList list = new ArrayList();
            list.AddRange(new car[]{new car("gsgdg",342,"BXdDE"), new car("afaf",242342,"BMWZ8")});
            Console.WriteLine("The elements of array LIST are:");
            foreach(car c in list)
            {
                Console.WriteLine("{0},{1},{2}", c.models,c.names,c.nums);
            }
          
        }
        public ArrayList list3 = new ArrayList();
        public void list2()
        {
         
            list3.Add("Item1");
            list3.Add("Item2");
            list3.Add("Item3");
            list3.Insert(0,"Item4");
            list3.Reverse();
            Console.WriteLine("The elements of array LIST2 are:");
            foreach(string s in list3)
            {
                Console.WriteLine("{0}", s);
            }
            ArrayList arlst = new ArrayList();
            Console.WriteLine("The elements of array LIST3 are:");
            arlst.AddRange(list3);
            foreach (string s in arlst)
            {
                Console.WriteLine("{0}", s);
            }
        }
    }
    class car
    {
        public string names, models; public int nums;
        public car(string name, int num, string model)
        {
            names = name;
            nums = num;
            models = model;
        }
    }
  
}

No comments:

Post a Comment