Friday, 3 October 2014

Array list and Hash table

1.ArrayList  represents the ordered collection of an object or it can be said that it is individually indexed where as Hash table uses a key to access the elements in the collection.
2.ArrayList is used when you want to access the elements by using index where as Hash table is used when you must access elements by using an index.
Array list
Hash table
using System;
using System.Collections;
class Program
{
  static void Main()
    {
// Create an ArrayList and add two ints.
ArrayList list = new ArrayList();
        list.Add(5);
        list.Add(7);
// Use ArrayList with method.
Example(list);
    }

    static void Example(ArrayList list)
    {
        foreach (inti in list)
        {
        Console.WriteLine(i);
        }
    }
}Output
5
7
using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtablehashtable = new Hashtable();
hashtable[1] = "One";
 hashtable[2] = "Two";
hashtable[13] = "Thirteen";
foreach (DictionaryEntry entry in hashtable)
{
     Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}
}
}
Result
13, Thirteen
2, Two
1, One



No comments:

Post a Comment