[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


arraylist vs hashtable?

Posted on 2006-05-19
Medium Priority
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
  • 2

Expert Comment

ID: 16716461

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

ID: 16716529
Is a hashtable just a 2d array?
Cant you add and remove objects from an arraylist?

Expert Comment

ID: 16716628
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

dstanley9 earned 80 total points
ID: 16717941
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 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