Array of Arrays

  • Thread starter Thread starter Hugh
  • Start date Start date
H

Hugh

Hi there,

How to dim a array of arrays and index it? Let's say,
array of 10 elements with each element is a (8, 12) 2-D
array. Thanks in advance for your help.

Hugh
 
Hugh said:
Hi there,

How to dim a array of arrays and index it? Let's say,
array of 10 elements with each element is a (8, 12) 2-D
array. Thanks in advance for your help.

Hugh

Have u tried 'Dim intArray(8, 12)() As Integer'?

Hope this helps :)

Mythran
 
Hugh,
Try something like:

Dim elements(10 - 1)(,) As Integer


Note you need to explicitly initialize each element of the array:

For index As Integer = 0 To elements.Length - 1
ReDim elements(index)(8 - 1, 12 - 1)
Next

To initialize every element of the inner arrays, you can use something like:

Dim rng As New Random
For index As Integer = 0 To elements.Length - 1
Dim element(,) As Integer = elements(index)
For row As Integer = element.GetLowerBound(0) To
element.GetUpperBound(0)
For column As Integer = element.GetLowerBound(1) To
element.GetUpperBound(1)

element(row, column) = rng.Next(1, 10)
Next
Next
Next

Remember that arrays are reference types, so element refers to the specific
inner array, not a copy of the inner array...

Alternatively you can access one of the inner array elements directly:

elements(index)(row, column) = rng.Next(1, 10)

Hope this helps
Jay
 
Mytrhan,
Wouldn't that be a 2-D array of arrays of integers, as opposed to an array
of 2-D arrays of integers?

A 2-D array of arrays of integers:
Dim intArray(8, 12)() As Integer

A 10 element array of 2-D arrays of integers:
Dim elements(10 - 1)(,) As Integer

I'd be curious on which Hugh actually meant.

Jay
 
Thank you very much, Jay and Mytrhan. What I wanted was a
10 elements of 1D array with each element is a 2D array.
I think Jay is right. Thank you again. I have been busy
at other things and have not had chance to try out yet.

Hugh
 
Hugh said:
Thank you very much, Jay and Mytrhan. What I wanted was a
10 elements of 1D array with each element is a 2D array.
I think Jay is right. Thank you again. I have been busy
at other things and have not had chance to try out yet.

Hugh

Glad I didn't gamble anything :)

Mythran
 

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