redim嵌套数组

时间:2012-11-20 09:38:14

标签: arrays vba access-vba multidimensional-array

我有一个数组路径...这个数组包含一个嵌套数组路径列表。它可能看起来像这样:

path( 0  1  2  3  4  5  6  7  8 )
"1" | 1, 1, 1, 1, 1, 1, 1, 1, 1 |
"2" | 4, 3, 1, 4, 2, 3, 4, 3, 2 |
"3" | 1, 1,  , 2, 1, 2, 3, 3, 2 |
"Val" A, B, C, D, E, F, G, H, I

现在我有一个小循环来获得第二行的最大值。

x = 1
For c = 0 To UBound(path)
   If IsArray(path(c)) Then
        If CInt(path(c)(x)) <= maxDimension1 Then
        maxDimension1 = CInt(path(c)(x))
        End If
    End If
Next
redim preserve pathValues(maxDimension1 - 1)

我现在必须找到行“2”中元素的最大元素数,并将pathValues中的array-Element重新加到此处。 我试过了:

For Dimension2 = 1 To maxDimension1
    For c = 0 To UBound(Path)
        If IsArray(Path(c)) Then
            If CInt(Path(c)(x)) = Dimension2 Then
                If CInt(Path(c)(2)) >= maxDimension2 Then
                maxDimension2 = CInt(Path(c)(2))
                End If
            End If
        End If
    redim PathValues(c)(maxDimension2) //Syntax Error
    next
next

有没有办法避免多维数组的变通方法? 解释:pathValues最终看起来像这样:

PathValues() = (C,(E, I),(B, F, H),(A, D, G))

1 个答案:

答案 0 :(得分:0)

我通过递归调用一个使用x作为“深度”的函数来修复它,并使用完整路径创建一个包含空元素的单个数组,以便稍后写入这些值。 一个只需要添加一个语句来整理你不想要的上限,因为它们属于其他数组。其他一切都很好

Function iterate_Path(path As Variant, x As Integer, value_x As Variant) As Variant
Dim insideArray, returnPath
returnPath = Array()

For c = 0 To UBound(path)
    If IsArray(path(c)) Then
       If CInt(path(c)(x)) = value_x Then
            If x <> UBound(path(c)) Then
                If CInt(path(c)(x)) > UBound(returnPath) + 1 Then
                    ReDim Preserve returnPath(CInt(path(c)(x)) - 1)
                End If
                returnPath(path(c)(x) - 1) = iterate_Path(path, x + 1, path(c)(x))
            ElseIf CInt(path(c)(x)) > UBound(returnPath) + 1 Then
                ReDim Preserve returnPath(CInt(path(c)(x)) - 1)
            End If
       ElseIf CInt(path(c)(x)) > UBound(returnPath) + 1 Then
            ReDim Preserve returnPath(CInt(path(c)(x)) - 1)
            If x + 1 = UBound(path(c)) Then
            returnPath(CInt(path(c)(x)) - 1) = iterate_Path(path, x + 1, path(c)(x))
            End If
       ElseIf x + 1 = UBound(path(c)) Then
            If CInt(path(c)(x)) > UBound(returnPath) + 1 Then
                ReDim Preserve returnPath(CInt(path(c)(x)) - 1)
            End If
            returnPath(CInt(path(c)(x)) - 1) = iterate_Path(path, x + 1, path(c)(x))
       End If
    Else
        returnPath(CInt(path(c)(x)) - 1) = Empty
    End If
Next
iterate_Path = returnPath
End Function