Arraylist vs Array vs DataTable

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Wondering the best way of storing my data.

This is data pulled and stored in the database but a lot of data
manipulation goes on the client side. I never have to remove items but
will sometimes clear them all out. It seems to me the best way,
resource wise, would be just a string array and then use redim when I
have to add a new "row" of data. Arraylist & datatables seem easier to
work with, but since I don't need much of the extra's that they add, the
overhead seems too much price to pay...

Any thoughts?
Chris
 
It depends a lot on the amount of data you are talking about and such. I
have excellent results with arraylists, swear by them. I also find
hashtables quite nice. From an overhead perspective - I have stored
thousands of objects in an arraylist before and it moves along quite fast.
If you need to sort, search, etc - super fun there. As for quick access to
an object, hashtables also have their great moments.

For situations where a flexible size was important, I suggest arraylist, but
if you are pulling down data from SQL Server or similar, a datatable will
provide you a lot of stuff specificly useful to the server.

HTH
Derek
 
Derek said:
It depends a lot on the amount of data you are talking about and such. I
have excellent results with arraylists, swear by them. I also find
hashtables quite nice. From an overhead perspective - I have stored
thousands of objects in an arraylist before and it moves along quite fast.
If you need to sort, search, etc - super fun there. As for quick access to
an object, hashtables also have their great moments.

For situations where a flexible size was important, I suggest arraylist, but
if you are pulling down data from SQL Server or similar, a datatable will
provide you a lot of stuff specificly useful to the server.

HTH
Derek

Like I said, I don't need any of the sorting, searching and extras that
those object give me. Just need a place to store the data while we are
editing it. I was just kinda curious if there was much overhead there
was of using ArrayLists vs Arrays. I mean I assume that Arraylist deep
down is just an array and is just doing a redim when I do an add. So
why should I hold onto all the overhead for a simple datastore.

Thanks for your thoughts.
Chris
 
You will have to pay a price when ReDim'ing an array as that will
create a 'new' array and copy all the elements to it. If you like
working with Array semantics, then an ArrayList is a good choice. If
you know you'll be working with strings, you might take a look at the
StringCollection class in the System.Collections.Specialized namespace.
 
Chris,

Is this for a commodore 64, this kind of problems is in my opinion not for
today anymore (or is should be for a PDA).

The only problem I can think of is about deleting very much rows in one
time, because that is a crime in a datatable in VS2002/2003.

For the rest take the one which makes your program the much readable.

Data from a database; datatable
Dynamic array; arraylist
Static array; array

Just my thought,

Cor
 
Chris said:
You will have to pay a price when ReDim'ing an array as that will
create a 'new' array and copy all the elements to it. If you like
working with Array semantics, then an ArrayList is a good choice. If
you know you'll be working with strings, you might take a look at the
StringCollection class in the System.Collections.Specialized namespace.

If I:

Dim Array(4) as String
.... Add Data to Array
ReDim Preserve Array(5)

All 5 of the items in Array get copied over when I redim? I didn't know
that. Do I take the same hit useing an ArrayList.Add()?

Chris
 
Cor said:
Chris,

Is this for a commodore 64, this kind of problems is in my opinion not for
today anymore (or is should be for a PDA).

The only problem I can think of is about deleting very much rows in one
time, because that is a crime in a datatable in VS2002/2003.

For the rest take the one which makes your program the much readable.

Data from a database; datatable
Dynamic array; arraylist
Static array; array

Just my thought,

Cor


"The only problem I can think of is about deleting very much rows in one
time, because that is a crime in a datatable in VS2002/2003."
Can you explain this a bit more?


"Is this for a commodore 64, this kind of problems is in my opinion not
or today anymore (or is should be for a PDA)."
I do have to be concerned about memory in this application. It is going
to be running on TS, so I don't want to eat memory and other resources
just becauses it is easier....

Chris
 
You don't need to, no. The array is probably better than the array list
with < 1000 items or so, depending on how you use it. The arraylist is just
allocating more storage than you need so in the average case it doesn't need
to ReDim when adding an item.

I tend to store database objects in hashtables, keyed on the database
primary key of course. If you are wiping the entire table though and don't
care about keys, then I would suggest an arraylist. It will scale better
than a basic array and isn't any more difficult to use. The only thing is
boxing/unboxing, which might slow you down a tiny bit - but not in any
noticable way I don't think.
 
Some good advice Cor, overhead and memory consumption should always be taken
into consideration, but they aren't make or break like they once were.

Derek
 
Like I said, it's your call there really Chris. Arraylist does derive from
array on down in the plumbing, but the overhead issue isn't so much with the
object's added properties but what you're doing with the data - if you can
use the features of Arraylist without Redimming, go for it - I hate
redimming just cause I was taught that way for whatever that's worth :-)

Derek
 

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

Back
Top