?
Solved

Array of Arrays

Posted on 2005-04-21
7
Medium Priority
?
514 Views
Last Modified: 2010-04-23
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
Comment
Question by:heartland
7 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
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

by:iboutchkine
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"}

        ' 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
 

Author Comment

by:heartland
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:heartland
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

by:graye
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

by:heartland
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

by:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: jpaulino
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

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question