C# ArrayList

ArrayList is stretchable. You can append, remove, or insert item easier than a normal array.
ArrayList stores items of object type. Although you add numbers or string values, they will be converted automatically to object type before storing them. You can convert object items back to string type by using toString() method.
How to convert them back to numbers?
Answer: apply int.parse() or int32.parse() method to the items of string type.

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

namespace Csharp_exercises
{

   class Program
   {
    
     static void Main(string[] args)
     {
        ArrayList ls = new ArrayList();
        int i;
        ls.Capacity = 10;//set number of elements that arraylist can store
        //add items to arraylist
        ls.Add(12);
        ls.Add(30);
        ls.Add(20);
        ls.Insert(2,80); //insert item to a specific location
        ls.Sort();//sort arrarylist in ascending order if you wan to sort the arraylist in descending order
        //use reverse method after using sort method
        //read items from arraylist
        //reverse the arraylist to sort it in descedning order
        ls.Reverse();
           for(i=0;i<ls.Count ;i++) //get number of arraylist elements by Count property
           Console.WriteLine (ls[i].ToString());
        //use removeat(index) method to remove a specific item
        //remove all items of the arraylist with clear method
        
        ls.RemoveAt(2);
        ls.Clear();
        //wait for key press
        Console.Read();

     }
}
}

Posted by: Dara | post date: 01-25-2013 | Subject: C# Programming Language




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.