Binary Search an ArrayList : ArrayList « Collections Data Structure « C# / C Sharp






Binary Search an ArrayList

  

using System;
using System.Collections;

class Album : IComparable, ICloneable {
    private string _Title;
    private string _Artist;

    public Album(string artist, string title) {
        _Artist = artist;
        _Title = title;
    }

    public string Title {
        get {
            return _Title;
        }
        set {
            _Title = value;
        }
    }

    public string Artist {
        get {
            return _Artist;
        }
        set {
            _Artist = value;
        }
    }
    public override string ToString() {
        return _Artist + ",\t" + _Title;
    }
    public int CompareTo(object o) {
        Album other = o as Album;
        if (other == null)
            throw new ArgumentException();
        if (_Artist != other._Artist)
            return _Artist.CompareTo(other._Artist);
        else
            return _Title.CompareTo(other._Title);
    }
    public object Clone() {
        return new Album(_Artist, _Title);
    }
}

class TitleComparer : IComparer {
    public int Compare(object l, object r) {
        Album left = l as Album;
        Album right = r as Album;
        if ((left == null) || (right == null))
            throw new ArgumentException();
        if (left.Title != right.Title)
            return left.Title.CompareTo(right.Title);
        else
            return left.Artist.CompareTo(right.Artist);
    }
}
class Class1 {
    static void Main(string[] args) {
        ArrayList arr = new ArrayList();

        arr.Add(new Album("G", "A"));
        arr.Add(new Album("B", "G"));
        arr.Add(new Album("S", "A"));

        arr.Sort();
   
        try {
            foreach (Album a in arr) {
                Console.WriteLine(a);
            }
        } catch (System.InvalidCastException e) {
        }

        arr.Sort(new TitleComparer());
        foreach (Album a in arr) {
            Console.WriteLine(a);
        }

        Album l = new Album("L", "G");
        arr.Sort();
        int index = arr.BinarySearch(l);
        Console.WriteLine(index.ToString());
        arr.Sort(new TitleComparer());
        index = arr.BinarySearch(l, new TitleComparer());
        Console.WriteLine(index.ToString());
    }
}

           
         
    
  








Related examples in the same category

1.Add items to ArrayList and use foreach loop to check
2.Add array to ArrayList
3.Remove range from ArrayList
4.Clone an ArrayList
5.CopyTo, ToArray(), ToArray(typeof(String))
6.Using foreach to loop through ArrayList and use Indexer of ArrayList
7.Check InvalidCastException when using foreach loop with ArrayList
8.Inserting into an ArrayList by index
9.Checks time needed for list operations using an ArrayList implementationChecks time needed for list operations using an ArrayList implementation
10.ArrayList Demo: hold classArrayList Demo: hold class
11.illustrates the use of ArrayList properties and methodsillustrates the use of ArrayList properties and methods
12.illustrates the use of an ArrayList that contains objects of the Car classillustrates the use of an ArrayList that contains objects of the Car class
13.illustrates the use of ArrayLists 2illustrates the use of ArrayLists 2
14.Convert an ArrayList into an arrayConvert an ArrayList into an array
15.Sort and search an ArrayListSort and search an ArrayList
16.Demonstrate ArrayListDemonstrate ArrayList
17.Search for the first occurrence of the duplicated value
18.Search for the first occurrence of the duplicated value in the last section of the ArrayList
19.Search for the first occurrence of the duplicated value in a section of the ArrayList
20.Search for the first occurrence of the duplicated value in a small section at the end of the ArrayList
21.Convert a ArrayList object to a array
22.Represents a loose abstraction of an arraylist, wheres the buffer array is available for public access.