Dynamic arrays

  • Thread starter Thread starter Dusty
  • Start date Start date
D

Dusty

How can I make my dynamic array ? I'm curios how the
ArrayList does it.
For example:
string[] s = new string[16];

and later I want to expand this array to more than 16
elements without loosing previous data (like ReDim in
VB).

Any ideas ?
 
Any ideas ?

Allocate a new, larger array, copy over the existing elements, and
drop the reference to the old array. That's what ReDim does, and I'm
guessing that the ArrayList does something similar.



Mattias
 
If you look at an ArrayList instaince in the debugger's Watch window you'll
descover that it has an internal (private?) member called _items and it's
just an array of Objects. So I would imagine Mattias' guess is correct -
ArrayList allocates/reallocates regular arrays behind the scenes, however
that process may be optimized in a way...

Val.
 
Hello,

ArrayList is a dynamic array. You should add elements to it by using its
Add method. There are two properties that you can check to get its size:
.Count and .Capacity. The capacity will always be equal to or higher than
the count. You could think of it as similar to a vector class. If you want
to trim it so that the capacity is the count, you can call its TrimToSize()
method.

I hope that helped.

--------------------

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
Back
Top