Question

So I want to create an array of arrays and from what I gather, a jagged array is what I need.

I have an array x(i,j) which defines a square nxn matrix of integers. For every iteration of k, two integer values inside this array are swapped to try and better the matrix x(i,j). I need to create an array which stores this matrix x(i,j) after every iteration of k.

To clarify, say if I have

1  2  3
4  5  6
7  8  9

And I perform an iteration swapping two elements inside the array:

7  2  3
4  5  6
1  8  9

I want to be able to store these arrays inside an array to be recalled any time. I've attempted a solution here:

Dim y() As Variant 'Declare as a variant to contain arrays
ReDim y(1 To IterationLimit) 'This will be the jagged array

For k = 1 To IterationLimit
'Some code goes here for the swap
    y(k) = x(i,j)
next k

Now say I want the 85th iteration. I want to be able to type y(85) [or similar] to pull up the matrix x(i,j) for that particular time.

Hopefully i've explained this well enough. Any help is appreciated, i'm really stuck on this.

edit: deleted code

Was it helpful?

Solution

OK. I think you may simply not be assigning the arrays to y correctly. As I mentioned in the comments:

y(k) = x(i,j) is ONLY storing the value represented by i/j coordinates in the x array.

In order for y(k) to refer to the entire array x at that time, you would do:

y(k) = x

If that is still not functioning, then perhaps something else is incorrect. Here is an example where I have a 2x2 (base 0) array called baseArray, which I explicitly fill with some values. I iterate from i = 0 to itLimit, and within each iteration, I iterate over the items in the array, multiplying the values by 2, and storing in an array variable tmpArray. After the values have been transformed, I store them in arrContainer, and proceed to the next For i = 0 to itLimit iteration.

Sub FunWithArrays()
Dim itLimit As Integer  '## Iteration limit.'
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim baseArray(2, 2) As Variant '## an example array.'

    '## Put some dummy data in this array.'
    baseArray(0, 0) = 1
    baseArray(0, 1) = 65
    baseArray(0, 2) = 13
    baseArray(1, 0) = 14
    baseArray(1, 1) = 29
    baseArray(1, 2) = 44
    baseArray(2, 0) = 9
    baseArray(2, 1) = 16
    baseArray(2, 2) = 33

Dim tmpArray(2, 2) As Variant '## This will temporarily hold values as we transform them.'
Dim arrContainer() As Variant '## an array of arrays, to store the iteration arrays.'

    itLimit = 2  '## set a max iteration.'

    For i = 0 To itLimit
        '## Transform the base array somehow'
        For j = LBound(baseArray, 1) To UBound(baseArray, 1)
            For k = LBound(baseArray, 2) To UBound(baseArray, 2)
                tmpArray(j, k) = baseArray(j, k) * 2
            Next
        Next
        ReDim Preserve arrContainer(i)
        arrContainer(i) = tmpArray
    Next

    Dim y As Variant
    '## Now, refer to a single iteration stored in the arrContainer variable:

    y = arrContainer(2)

End Sub

In this first screenshot, I use the Locals window to review the variables and what they contain. After the first iteration, you can see that tmpArray has been populated, and is the same dimensions as baseArray, but the values therein have been multiplied by 2.

Screenshot after the first iteration

Examining the arrContainer variable, we see that it has only 1 item, and that item is an array equal to the tmpArray that we created in the iteration, above.

Screenshot of arrContainer after the first iteration

After the last iteration, we can review arrContainer and see that it contains 3 items (0 to 2, per our For i to itLimit loop). Each array inside arrContainer is equal to one of the arrays created in the iteration above.

Screenshot of arrContainer array of arrays after all iterations

We can now refer to these items, like:

Dim y as Variant y = arrContainer(2) '# or any in bounds index could be used instead of "2"'

And finally using the VBE Locals window to review y:

Final screenshot referring to a single array item within arrContainer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top