Tuesday 21 August 2018

C#: ArrayList

ArrayList is used to store list of elements, one advantage of the ArrayList is, it is dynamic in size. It will grow on elements addition and shrinks on elements removal.

How to declare ArrayList?
Syntax
ArrayList listName = new Arraylist();

Example
ArrayList names = new ArrayList()

How to add elements to ArrayList?
By using ‘add’ method you can add elements to array list. Unlike arrays, you can add any kind of data to array list.

Syntax
listName.add(element);

Example
names.Add("Hari Krishna Gurram");
names.Add(true);
names.Add(123);
names.Add(123.5);

Program.cs

using System;
using System.Collections;

namespace collections
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList names = new ArrayList();

            names.Add("Hari Krishna Gurram");
            names.Add(true);
            names.Add(123);
            names.Add(123.5);

            Console.WriteLine(names[0]);
            Console.WriteLine(names[1]);
            Console.WriteLine(names[2]);
            Console.WriteLine(names[3]);
        }
    }
}

Output

Hari Krishna Gurram
True
123
123.5

Following tables summarizes important properties and methods provided by ArrayList. If you want to know about all the method, properties go through following documentation.


Property
Description
Capacity
Gets or sets the number of elements that the ArrayList can contain.
Count
Gets the number of elements actually contained in the ArrayList.

Method
Description
Add(Object)        
Adds an object to the end of the ArrayList.
AddRange(ICollection)
Adds the elements of an ICollection to the end of the ArrayList.
BinarySearch(Object)
Search for an element in the collection and return the index of the element if found, else return –ve number.
Clear()
Remove all the elements from the list
Clone()
Creates shallow copy of the list
Contains(Object)
Return true if item is found in the ArrayList, otherwise, false.
CopyTo(Array)
Copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array.
IndexOf(Object)
Return the index of the element in the collection if exists, else return -1.
Insert(Int32, Object)
Inserts an element into the ArrayList at the specified index.
LastIndexOf(Object)
Return last occurrence index of given element if it exists, else -1.
Remove(Object)
Remove the first occurrence of the given element from list.
Reverse()
Reverses the order of the elements in the entire ArrayList.
Sort()
Sorts the elements in the entire ArrayList.
ToArray()
Copies the elements of the ArrayList to a new Object array.


Program.cs

using System;
using System.Collections;

namespace collections
{
    class Program
    {

        public static void printArray(ArrayList list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }
        }

        static void Main(string[] args)
        {
            ArrayList names = new ArrayList();

            names.Add("One");
            names.Add("Two");
            names.Add("Three");
            names.Add("Four");

            printArray(names);

            Console.WriteLine("\nReversing Elements\n");
            names.Reverse();
            printArray(names);

            Console.WriteLine("\nSort Elements\n");
            names.Sort();
            printArray(names);
        }
    }
}

Output

One
Two
Three
Four

Reversing Elements

Four
Three
Two
One

Sort Elements

Four
One
Three
Two


Previous                                                 Next                                                 Home

No comments:

Post a Comment