Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Length Array 1

Status
Not open for further replies.

troelssmit

Programmer
Oct 20, 2004
12
DK
Hi,

How can I declare an array of a datatype, whose size might have to change during program execution ?

- int[8] myint; // can't cange size
- int[] myint = new {8,7,6,5,4,3,2,1} //can't change size
- ArrayList myint = new ArrayList(); //can hold various kind of object, and so is giving my program an overhead. I only need one datatype.
 
I allocate more than I need and then when out of space I copy to a new doubled size.

if (++lngCount >= aryLine.Length;
Common.ReDimP (2 * lngCount + 2, ref aryLine);
aryLine[lngCount] = objTs.ReadLine();


internal static void ReDimP(int nbr, ref int[] ary)
{
int n = 0;
// allocate new size
int[] aryn = new int[nbr];
// get current size
n = (ary == null)? 0: ary.Length;
if (nbr < n) n = nbr;
if (n > 0)
Array.Copy(ary, aryn, n);
ary = aryn;
}

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Wouldn't an ArrayList be better for this?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I am already using an ArrayList, so I was simply wondering if there is a better way to do it, Since I know which datatype Ill be using.
 
The requirements of strong datatyping and variable length are incompatible to a certain degree.

When VS.NET 2005 comes out with generics, then you'll be able to store your datatype in a collection (ArrayList) and not have to cast it when you read it out. Until then, you can wrap the ArrayList in your own type that does the casting automatically.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
That is a very good idea!
I still have the problem that I don't know the implementation of ArrayList, so I don't know what kind of overhead I am creating, using it instead of a normal array.
 
How much data do you plan to store in it?

How much "churn" do you expect the data to have (ie. is the data fairly static?)

If you like, you could write a quick and dirty program to test it by putting 100,000 objects in one.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
The data is fairly static, but I need to be able to access it very fast. Running through each object and asking it a couple of simple questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top