Programming References and tutorials for Developers.

c# ArrayList Class

By The Saint on Thursday, January 29, 2009

Filed Under:

c# ArrayList Class
Static arrays are not very useful when the size of an array is unknown in advance or is likely to change during the lifetime of a program. One solution to this problem is to use a type of array that automatically resizes itself when the array is out of storage space. This array is called an ArrayList and it is part of the System.Collections namespace in the .NET Framework library.

An ArrayList object has a Capacity property that stores its size. The initial value of the property is 16. When the number of elements in an ArrayList reaches this limit, the Capacity property adds another 16 elements to the storage space of the ArrayList. Using an ArrayList in a situation where the number of elements in an array can grow larger, or smaller, can be more efficient than using ReDim Preserver with a standard array.

Members of the ArrayList Class
The ArrayList class includes several methods and properties for working with ArrayLists. Here is a list of some of the most commonly used methods and properties:

- Add(): Adds an element to the ArrayList.
- AddRange(): Adds the elements of a collection to the end of the ArrayList.
- Capacity: Stores the number of elements the ArrayList can hold.
- Clear(): Removes all elements from the ArrayList.
- Contains(): Determines if a specified item is in the ArrayList.
- CopyTo(): Copies the ArrayList or a segment of it to an array.
- Count: Returns the number of elements currently in the ArrayList.
- GetRange(): Returns a subset of the ArrayList as an ArrayList.
- IndexOf(): Returns the index of the first occurrence of the specified item.
- Insert(): Insert an element into the ArrayList at a specified index.
- InsertRange(): Inserts the elements of a collection into the ArrayList starting at the specified
index.

Example:


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

namespace ArraysBlogs
{
class Program
{
static void Main(string[] args)
{
//Creating an object 'list' derived from ArrayList class
ArrayList list = new ArrayList(16);

//Insertin value 1 and 2 to arraylist 'list'
list.Add(1);
list.Add(2);

//Accessing the value in an Arraylist 'list'
//displaying value 1 and 2 on the screen

Console.WriteLine(list[0]);
Console.WriteLine(list[1] + "\n");

//Creating an integer array a
int[] a = new int[10];

//Inserting a value 10,20,30 to array 'a'
a[0] = 10;
a[1] = 20;
a[2] = 30;

//Adding range of elements from array 'a' to Arraylist 'list'
list.AddRange(a);

//Displaying the value of Arraylist list using foreach()

foreach (object getList in list)
{
Console.WriteLine(getList);
}

//Getting the capacity of an Arraylist 'list'
//It will display 16 as it was specified.

Console.WriteLine("Capacity of list: " + list.Capacity);
Console.ReadKey();
}
}
}


2 comments for this post

Anonymous

hehehe... ayus ah... damu na traffic. keep it up!

Posted on January 30, 2009 at 9:37 AM  
Anonymous

sir? may ym ka? anu ym mo?

Posted on February 2, 2009 at 11:13 PM