Reader Level:
Articles

Binary Search Using ArrayList

By Mahesh Chand on January 10, 2000
The ArrayList class provide binary search algorithm and more...
  • 0
  • 0
  • 82890

Tools Used Visual C# .NET
Assembly mscorlib.dll
Namespace System.Collections

Recently, I need to use binary search in my applications and I found ArrayList class very handy to do so. 
The ArrayList class Implements a collection of objects using an array whose size is dynamically increased as required. The ArrayList class is very useful if you are playing with arrays and need to add, remove, indexed, or search data in a collection.
Adding data to an ArrayList
Adding data to an ArrayList is pretty easy. Just call Add member of the class:

ArrayList dataArray =
new ArrayList();
for ( int i = 0; i<20; i++ )
{
dataArray.Add( i );
}

or:
dataArray.Add( "Object type" );

The Add member takes an argument of Object type. You can add any value to the array.

Delete data from an ArrayList

The Remove and RemoveAt members are used to delete data from an ArrayList
// Remove the element containing "3".
myAL.Remove( "3" );

Searching elements in ArrayList

The BinarySearch member of the ArrayList was very useful for me. This member returns the zero-based index of a specific element in the sorted ArrayList or a portion of it, using a binary search algorithm.
int myIndex=myList.BinarySearch( 3 );
That's it..  The ArrayList class is much more fun than this. See documentation for more details.

Mahesh Chand

Mahesh Chand is founder of C# Corner. C# Corner founded in 1999 is a FREE member contributions based open platform for developers to solve problems, learn new technology and hang out.  Mahesh has been awar... Read more

COMMENT USING

Trending up