Introduction
The ArrayList class provides a number of
properties and methods that are used to work with an ArrayList. In this article,
I'm going to describe three important methods of the ArrayList class, which are:
- BinarySearch(): Performs a binary search on the ArrayList.
- Sort(): Sorts the items in the ArrayList.
- Reverse(): Reverses items in the ArrayList.
Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)
BinarySearch()
The BinarySearch method of the ArrayList class implements
the binary searching algorithm. This uses a "divide and conquer" approach to
finding the correct element, and only works on a pre-sorted array. For this
reason, never use BinarySearch if your ArrayList might not be sorted.
The BinarySearch method returns the 0-based
index of the object if it's found. If the object is not found in the list,
BinarySearch returns –1.
Code
using
System;
using
System.Collections.Generic;
namespace
BinarySearchmethod
{
class AKSHAY
{
static void
Main(string[] args)
{
List<string>
list = new List<string>()
{ "A", "B",
"C", "D",
"E", "F",
"G" };
int index1 = list.BinarySearch("C");
Console.WriteLine("Index
1: {0}", index1);
int index2 = list.BinarySearch("F");
Console.WriteLine("Index
2 : {0} ", index2);
int index3 = list.BinarySearch("H");
Console.WriteLine("Index
3 : {0} ", index3);
// wait for input before exiting
Console.WriteLine("Press
enter to finish");
Console.ReadLine();
}
}
}
Output
Sort()
Sorting an ArrayList is done in many programs that
use ArrayList, as sorting provides a view of the data that is helpful for both
users and computers. The Sort method in the base class libraries is a
parameterless instance method on ArrayList.
The ArrayList.Sort() used Quick algorithm to sort
the element of Arraylist class. The QuickStort algorithm is a comparison sort
(also called an unstable sort), which means that a "less than or equal to"
comparison operation determines which of two elements should occur first in the
final sorted list. However, if two elements are equal, their original order
might not be preserved. In contrast, a stable sort preserves the order of
elements that are equal. To perform a stable sort, you must implement a custom
IComparer interface to use with the other overloads of this method.
When we call Sort() on the ArrayList, the default
implementation of IComparer is called which uses QuickSort. QuickSort calls the
IComparable implementation of CompareTo() on each of your objects in the
ArrayList.
Code
using
System;
using
System.Collections;
class
akshay
{
static void
Main()
{
// Create an ArrayList with 4 strings.
ArrayList list =
new ArrayList();
list.Add("Manesh");
list.Add("Akshay");
list.Add("Vikash");
list.Add("Anuj");
list.Add("Dharmesh");
list.Add("Raman"):
// Get all elements in ArrayList.
Console.WriteLine("The
ArrayList initially contains the following values...");
foreach (string
value in list)
{
// Display the elements.
Console.WriteLine(value);
}
// sort the elements.
list.Sort();
Console.WriteLine("The
sorted array list is ...");
foreach (string
value in list)
{
// Display Sorted elements.
Console.WriteLine(value);
}
Console.Read();
}
}
Output
Reverse()
The ArrayList.Reverse() method are used to reverse
the order of element of the ArrayList class such that the element at ArrayList [i],
where i is any index within the range, moves to ArrayList [j], where j equals
index + index + count - i - 1. The Reverse method is Overloded. The simplest
form of this method uses the following declaration:
ArrayList.Reverse()
Code
using
System;
using
System.Collections;
class
akshay
{
static void
Main()
{
// Create an ArrayList with 4 strings.
ArrayList list =
new ArrayList();
list.Add("Manesh");
list.Add("Akshay");
list.Add("Vikash");
list.Add("Anuj");
list.Add("Dharmesh");
list.Add("Raman"):
// Get all elements in ArrayList.
Console.WriteLine("The
ArrayList initially contains the following values...");
foreach (string
value in list)
{
// Display the elements.
Console.WriteLine(value);
}
// Reverse the elements.
list.Reverse();
Console.WriteLine("The
sorted array list is ...");
foreach (string
value in list)
{
// Display
Reverse elements.
Console.WriteLine(value);
}
Console.Read();
}
}
Output
Resources