Solved

Array of Arrays

Posted on 2005-04-21
Medium Priority
514 Views
Hi Expert,

I have difficult to set values for an array of 2D arrays.  The code is as follow:

Private sub ArrayOfArrays()

Dim i, j, k, ArrOf1DArrays(3)(), ArrOf2DArrays(3)(,) As Integer

For i = 0 To 3
For j = 0 To 7
ReDim Preserve ArrOf1DArrays(i)(j)
For k = 0 To 11
ReDim Preserve ArrOf2DArrays(i)(j, k)
iArrOf2DArrays(i)(j, k) = i + j + k
Next
Next
Next
End sub

However, it works for array of 1D arrays: ArrOf1DArrays.  How do I make it work for an array of 2D arrays?  Please help.   Thanks in advance.

Hugh
0
Question by:heartland

LVL 86

Expert Comment

ID: 13838897
When you use Preserve with ReDim, you can only change the size of the last dimension so this line:

ReDim Preserve ArrOf2DArrays(i)(j, k)

is going to cause problems.

Explain in more detail what you are tyring to accomplish.

...or if the final size of the arrays are known, declare them as the correct size to begin with and then loop thru and populate them with the values you want.
0

LVL 28

Expert Comment

ID: 13843338
VB.NET support array which element of each is an array - known as jagged arrays

' this procedure tests arrays of arrays

Sub TestJaggedArray()
' Initialize an array of arrays.
Dim arr()() As String = {New String() {"a00"}, _
New String() {"a10", "a11"}, _
New String() {"a20", "a21", "a22"}, _
New String() {"a30", "a31", "a32", "a33"}}

' Show how you can reference an element.
Console.WriteLine(arr(3)(1))                    ' => a31

' Assign an entire row of elements.
arr(0) = New String() {"a00", "a01", "a02"}

Console.WriteLine(arr(0)(2))                    ' => a02

' Expand one of the elements.
ReDim Preserve arr(1)(3)
' Assign the new elements (now Nothing)
arr(1)(2) = "a12"
arr(1)(3) = "a13"
' Read back one of them
Console.WriteLine(arr(1)(2))                    ' => a12
End Sub
0

Author Comment

ID: 13843530
Hi Idle Mind,

Thanks for your reply.  What I want is a array of 2D arrays.  Each element of 2D arrays should have same dimensions. For example, each element should be 7x11 array for Arr(3)(,).
For Arr(i)(j,k), i and k may change and I know upper limits.  I think I can declare Arr(i)(j,k) with their upper limits. How do I do this and populate it?   Thanks.

Hugh
0

Author Comment

ID: 13846555
Hi iboutichkine,

Thanks for your help.  Hoever, I do not quite understand.  I do not need Jagged arrays. As what I explained in previous email, I need is an array of 2D arrays.  I can make array of 1D arrays working but not 2D arrays.

Hugh
0

LVL 41

Expert Comment

ID: 13847180
Yes you can have a jagged array of multidimensional arrays...  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfjaggedarrays.asp (this link is for C#, but the concept is the same)

However, somehow I don't think that's what you're after...  I'd recommend a simpiler approach

Dim i, j, k, ArrOf1DArrays(3, 7), ArrOf2DArrays(3, 7, 11) As Integer

For i = 0 To 3
For j = 0 To 7
ArrOf1DArrays(i, j) = i + j
For k = 0 To 11
ArrOf2DArrays(i, j, k) = i + j + k
Next
Next
Next
0

Author Comment

ID: 13858086
Hi graye,

Thanks for your help.  Yes, I could use 3D array as you suggested. However, I need to plug in 2D array in my application.  I need to use ArrOf2DArrays(0), ArrOf2DArrays(2) etc.. The reason for me to use array of 2D arrays is that it is easy to index its elements. I know that for what I want, array of 2D arrays and 3D array are the same.  However, it is difficult to index a 2D array if I use 3D arrays.  I was not familiar with array of arrays, and hoped that array of 2D arrays would work for me.

Thanks again.

Hugh
0

LVL 86

Accepted Solution

Mike Tomlinson earned 400 total points
ID: 13858223
I believe this is what you are looking for (I hope).  This produces a one dimensional array where each element is a two dimensional 7x11 array:

Private Sub ArrayOfArrays()
Dim i, j, k As Integer
Dim ArrOf1DArrays(3)(,) As Integer
For i = 0 To 3
Dim tempArray(7, 11) As Integer
For j = 0 To 7
For k = 0 To 11
tempArray(j, k) = i + j + k
Next
Next
ArrOf1DArrays(i) = tempArray
Next
End Sub
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month16 days, 13 hours left to enroll