arraylist vs hashtable?

Posted on 2006-05-19
Last Modified: 2010-05-18
what are the differences between the two?
i seem to be able to do a lot with arraylist (sort, reverse)
Question by:paulwhelan
    LVL 4

    Expert Comment


    The main difference is that the Hashtable is a list of key-value pairs, and the arraylist is a dynamically sized array.


    Say you want a list of User objects, but you want to access them in a list by their user name in stead of an integer index, you'd go for a Hashtable so you could access a user object like so:

    User myUser = (User) myHashTable["someusername"];

    If you just want an array that you don't need to resize to add and remove objects, you'd go for an ArrayList.

    Author Comment

    Is a hashtable just a 2d array?
    Cant you add and remove objects from an arraylist?
    LVL 4

    Expert Comment

    Well, i guess in some sense you could say it's something like a dynamically sized 2d array.

    You can remove and add object from any collection type.

    The MS docs om MSDN on these classes are very clear, simple to understand and also have good examples.

    For example if you search for ArrayList here:

    The first two results you get are these:

    And they explaint these kinds of things in great detail, with samples.

    The same goes for the Hashtable type :)
    LVL 25

    Accepted Solution

    Is a hashtable just a 2d array?

    No.  It's a 1d array indexed by a key instead of a number.  THe order of items in the HashTable is determined by the order of the hash values of the keys.

    If order is important, don't use a hashtable.  Use an ArrayList.  If you want a 2d array, use an ArrayList of ArrayLists if you don't know the dimensions at design-time or an Array[,] if you do.  If you want a sortable Array that is indexed by keys, store the keys in one array and the values in another using the Array.Sort(Array, Array) method to sort by the keys, making sure that the indexes stay in sync.


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now