ARTICLE
Binary Search Using ArrayList
The ArrayList class provide binary search algorithm and more...
| 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.