Array vs ArrayList

  • Thread starter Thread starter Davids
  • Start date Start date
D

Davids

is it true that I cannot dynamically add an item to an array? Eg

public char[] = {"a","b"};
char.Add("newitem");


Do I really have to switch to ArrayList to do this?
 
Arrays are fixed size. An ArrayList takes care of the resizing you would have to manage yourself but does so in a generaic way so isn't typesafe.

You can of course allocate an array larger than you know you need at the point of allocation and tehn fill the extra elements with the new stuff you discover. Or even better if you know the fina size of hte population allocate an array of this size upfront. But as to the original question? Yes its true, no you don't need to switch to an ArrayList if you're happy to manage the reallocation yourself.

Incidently, why are you so worried about switching to an ArrayList?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

is it true that I cannot dynamically add an item to an array? Eg

public char[] = {"a","b"};
char.Add("newitem");


Do I really have to switch to ArrayList to do this?
 
thx Richard
well I'm doing a hangman application and add upp the guessed letters to a
string Array. In one instance I have to search that particular array using
IndexOf() method but it is more complicated on ArrayList since there the
items are of object type...
 
Well I guess you know the maximum size of the array then (the number of letters in your alphabet) so just allocate an array of that size and just keep a track of the last index you inserted at

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

thx Richard
well I'm doing a hangman application and add upp the guessed letters to a
string Array. In one instance I have to search that particular array using
IndexOf() method but it is more complicated on ArrayList since there the
items are of object type...
 
Davids said:
well I'm doing a hangman application and add upp the guessed letters
to a string Array. In one instance I have to search that particular
array using IndexOf() method but it is more complicated on ArrayList
since there the items are of object type...

I don't see why that makes it any more complicated, so long as the
items implement Equals appropriately (as strings do).

I'm not sure I'd implement it that way myself though - are you just
trying to find out whether or not a particular letter has been guessed?
If so, I'd suggest an array of 26 bools (assuming a case-insensitive
Western alphabet with no accents).
 
just being obsessive and trying to have the code as simple as possible, I
see both Array and ArrayList are possible ways. This is how on learns c#,
this occurence forced me on finally reading about c# collections, thx for
your help!
 
Davids said:
just being obsessive and trying to have the code as simple as possible, I
see both Array and ArrayList are possible ways. This is how on learns c#,
this occurence forced me on finally reading about c# collections, thx for
your help!

To have the code as simple as possible, I'd seriously look at the array
of 26 bools approach - that's as simple as I can imagine...
 
yep agree with you and have done that. Bools... sounds similiar to booleans
but I know that isn't the case?!?
 
Davids said:
yep agree with you and have done that. Bools... sounds similiar to booleans
but I know that isn't the case?!?

No, they're the same. bool is shorthand for System.Boolean in C#, just
as int is shorthand for System.Int32.
 
Back
Top