Array of Arrays

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
heartlandAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
iboutchkineCommented:
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"}

        ' Read an element just added.
        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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
heartlandAuthor Commented:
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
 
heartlandAuthor Commented:
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
 
grayeCommented:
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
 
heartlandAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.