ArrayList versus HashTable

D

D. Shane Fowlkes

I've been reading up on Arrays in ASP.NET. I'm going to create an two
dimensional array of some type to contain 5 columns but a variable amount of
rows. I read up on the ArrayList function and the HashTable as well. In
the two books I have, there's no mention of the limitations of an ArrayList
and it can only contain a single column array. But from the examples, I've
seen, this seems to be true. The HashTable, as far as I can tell, can
contain key/pairs but can it contain 5 columns of data?

So considering my needs, would you suggest an old fashioned Array or try one
of the others mentioned above and why?

(I'm still working towards my "yellow belt" in .NET so I'm still pretty
new.)

Thanks in advance!
 
D

Darrin J Olson

In both a Hashtable (or Dictionary) and an Arraylist, you could store an
object as the value. The object could be a DataRow with 5 columns, or your
own object with 5 variables in it. The difference would be if you would like
to be able to find the correct entry by using a key in the key-value pair.
You could use some primary identifier as the key for each entry in the
hashtable to quickly find the entry with the correct 5 columns you are
looking for. With an Arraylist you would have to find the entry by matching
the exact object, or looping through them. Arraylists are real easy to use,
though, if you do not need the key for finding items.

I'm sure there are many more variables to make your decision on, but that's
what I look at mostly.

-Darrin
 
K

Kevin Spencer

I'm not sure where you're getting you information from, but columns have
nothing to do with either ArrayList or HashTable, or any other Collection
Type. A Collection is similar to an Array, with some added functionality,
depending upon the Collection Type. The Types ArrayList and HashTable are
Collections of type "Object" which means that they can contain any type of
object, and you can even put different types of objects into the same
Collection. Of course, using the Object type means Late Binding and poor
performance. Using Custom Typed Collections is recommended when using
Collections.

Collections are reference types while Arrays are value types, which means
that there is more processing and memory overhead in using Collections
instead of Arrays. However, if the additional functionality is desired, they
come in quite handy. In addition, Collections are not limited in length as
are Arrays (you can change the size of an Array, but it is costly, requiring
the creation of a new Array to hold the longer length).

You could use either Collections or Arrays in your case, but if you are
working with multiple "columns" of data, and the number is the same for each
element in the Collection/Array, you may find a multi-dimensional Array more
useful to you, as you would have to create an aggregate object to hold
multiple elements ("colum,ns") in a single Collection Item, such as structs
or arrays or classes. Another alternative would be to use a DataTable, which
you can build dynamically, adding colums and rows as needed.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top