What is Array List and Hash Tables?
Please Tell me With Examples.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Apr 11, 2012, 2:13 AM
Arraylist is a collection of objects(may be of different types).
Arraylist is very much similar to array but it can take values of different datatypes.If you want to find something in a arraylist you have to go through each value in arraylist, theres no faster way out.
Program that uses ArrayList [C#]
using System.Collections;
class Program
{
static void Main()
{
//
// Create an ArrayList and add three elements.
//
ArrayList list = new ArrayList();
list.Add("One");
list.Add("Two");
list.Add("Three");
}
}
Hashtable is also collection which takes a key corresponding to each values.
If you want to find something in a hashtable you dont have to go through each value in hashtable, instead search for key values and is faster.
Program that uses Hashtable [C#]
using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable[1] = "One";
hashtable[2] = "Two";
hashtable[13] = "Thirteen";
foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}
}
}
Please refer the below links
http://www.dotnetperls.com/arraylist
http://www.dotnetperls.com/hashtable
Thanks
Sam HobbsPosted Apr 11, 2012, 12:41 PM
Don't compare hash tables to ArrayLists; the Dictionary and SortedList classes are more relevant.
The way that a hash table works is that for each itme in th list, a hash is generated. The hash is not intended to be unique for each item; it just is supposed to determine where an item is approximately so it is not necessary to search all items. Other collections such as dictionaries store items in a manner such that items can be found quiclly and precisely and you do not need to add code to do that. With hash tables when a hash is not unique, you have to have code to deal with that.