Best way to Dim a jagged array.

  • Thread starter Thread starter Lance
  • Start date Start date
L

Lance

What is the best way to Dim a jagged array. For example,
if I want a 2D jagged array with a width of 16 and a
height of 4, it would be nice to be able to do the
following:

Dim myArray(3)(15) As Integer

Of course this causes a compile error. So, now I'm doing
something like this:

Dim myArray(3)() As Integer
For i as Integer = 0 to 3
Dim myArrayRow(15) As Integer
myArray(i) = myArrayRow
Next i

Is there a better way? (Note: For my needs you can
assume that all rows of the jagged array will be the same
width.)

Thank you,
Lance
 
What is the best way to Dim a jagged array. For example,
if I want a 2D jagged array with a width of 16 and a
height of 4, it would be nice to be able to do the
following:

Dim myArray(3)(15) As Integer

i tihnk you go Dim MyArray (3, 15)
 
Hi there,

You've got the simplest way there, Lance. :-)

If the secondary arrays are all the same size, why do you you call it a
"jagged" array - it's rectangular! If these arrays won't change in size, a
2-dimensional array is better. Any compelling reason why an array of arrays is
required?

DF
 
You've got the simplest way there, Lance. :-)

Does simplest mean fastest? I'm primarily concerned
about speed (which is why I'm using jagged arrays to
begin with).

The .NET framework is optimized for 1D arrays. Jagged
arrays can be handled as 1D arrays so jagged arrays are
significantly faster then the equivalent multi-
dimensional array.

Thanks for the reply!
Lance
 
Hi Lance,

Re: declaring and setting up a jagged array.
|| Does simplest mean fastest?

How often are you going to be creating these things?


|| The .NET framework is optimized for 1D arrays.
|| Jagged arrays can be handled as 1D arrays ...

They can ??

|| ... so jagged arrays are significantly faster then
|| the equivalent multi-dimensional array.

I wouldn't have thought that it's because they are treated like simple
arrays, surely, but the article 'Performance Tips and Tricks in .NET
Applications' mentions that jagged arrays are optimised better than
rectangular arrays.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html
/dotnetperftips.asp

But it's a matter of degree - I assume that 16x4 was just an example, but
how large <are> these arrays and how often are you going to be accessing them.
Is this going to be within the inner loop of something intensive? If not,
programming convenience is more valuable that a few microseconds of run time.

Regards,
Fergus
 
Hi Herfried,

]| The smiley looks like this:
]|
]| x x
]| ---

<That's> a smiley?? It looks like a grim, drunk robot! ;-)

Regards,
Fergus
 
Back
Top