Saturday, 2 May 2015

How to use C# ArrayList Class

ArrayList:-

Array lists are not strongly type collection. It will store values of different datatypes or same datatype. Array list size will increase or decrease dynamically it can take any size of values from any data type. These Array lists will be accessible with “System.Collections” namespace

ArrayList is one of the most flexible data structure from CSharp Collections. ArrayList contains a simple list of values. ArrayList implements the IList interface using an array and very easily we can add , insert , delete , view etc. It is very flexible because we can add without any size information , that is it will grow dynamically and also shrink .

Operations On ArrayList:-


 Add :
          Add method we use to append Items at the end of an ArrayList
  Insert :
          Insert method  we use Add Item in a specified position in an ArrayList
  Remove :
          Remove method we use to Item from ArrayList by specifying Item's name
  RemoveAt: 
          RemoveAt method we use to remove an item from a specified position
  Sort :
          To Sort Items in an ArrayList

Note :- All operation are operate on  the Object of ArrayList 

Source code Example



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Collections;

Namespace ConsoleDemoApp
{
    class Test
    {
        /* program to implement ArrayList in c# */

        public static void Main(string[] args)
        {
            int inputNum=0;

          
            Console.Write("\nEnter number of students to be add: ");
            inputNum= int.Parse(Console.ReadLine());

            ArrList arList = new ArrList();
            arList.ArrListOperations(inputNum);
            Console.ReadKey();
        }
    }

  
  

    Public class ArrList
    {
        public void ArrListOperations(int inputNum)
        {
          
            ArrayList ItemList = new ArrayList();
            for (int i=1; i<=inputNum; i++)
            {
            ItemList.Add("Item"+i);
            }

            // print
            Console.Write("\nEntered Items are: \n");
            for (int j = 0; j <= ItemList.Count-1; j++)
            {
               Console.Write(ItemList[j].ToString());
               Console.Write("\n");
            }

            //insert an item
            ItemList.Insert(3, "Item6");
            Console.Write("\nAfter inserting an item Item6 at 3 index: \n");
            for (int j = 0; j <= ItemList.Count - 1; j++)
            {
                Console.Write(ItemList[j].ToString());
                Console.Write("\n");
            }

            //sort itemms in an arraylist
            ItemList.Sort();
            Console.Write("\nAfter shorting ArrryList: \n");
            for (int j = 0; j <= ItemList.Count - 1; j++)
            {
                Console.Write(ItemList[j].ToString());
                Console.Write("\n");
            }


            //remove an item
            ItemList.Remove("Item1");
            Console.Write("\nAfter Removing item Item1: \n");
            for (int j = 0; j <= ItemList.Count - 1; j++)
            {
                Console.Write(ItemList[j].ToString());
                Console.Write("\n");
            }



            //remove item from a specified index
            ItemList.RemoveAt(3);
            // final ArrayList
            Console.Write("\n After Removing item at index 3, items are: \n");
            for (int j = 0; j <= ItemList.Count-1; j++)
            {
                Console.Write(ItemList[j].ToString());
                Console.Write("\n");
            }
        }
    }
}


Output:-


No comments:

Post a Comment