Arrays redim嵌套数组
我有一个数组路径。。。此数组包含嵌套数组路径的列表。它可能看起来像这样:Arrays redim嵌套数组,arrays,ms-access,multidimensional-array,vba,Arrays,Ms Access,Multidimensional Array,Vba,我有一个数组路径。。。此数组包含嵌套数组路径的列表。它可能看起来像这样: 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 UBoun
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)
有没有办法避免使用多维数组的变通方法?
解释:pathValues最终将如下所示:
PathValues() = (C,(E, I),(B, F, H),(A, D, G))
我通过递归调用一个函数修复了这个问题,该函数使用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
函数将路径(路径为变量,x为整数,值为变量)迭代为变量
昏暗的内部通道
returnPath=Array()
对于c=0到UBound(路径)
如果是IsArray(路径(c)),那么
如果CInt(路径(c)(x))=值
如果x UBound(路径(c)),则
如果CInt(路径(c)(x))>UBound(返回路径)+1,则
ReDim保存返回路径(CInt(路径(c)(x))-1)
如果结束
returnPath(路径(c)(x)-1)=迭代_路径(路径,x+1,路径(c)(x))
ElseIf CInt(路径(c)(x))>UBound(返回路径)+1然后
ReDim保存返回路径(CInt(路径(c)(x))-1)
如果结束
ElseIf CInt(路径(c)(x))>UBound(返回路径)+1然后
ReDim保存返回路径(CInt(路径(c)(x))-1)
如果x+1=UBound(路径(c)),则
returnPath(CInt(path(c)(x))-1)=迭代_path(path,x+1,path(c)(x))
如果结束
ElseIf x+1=UBound(路径(c))然后
如果CInt(路径(c)(x))>UBound(返回路径)+1,则
ReDim保存返回路径(CInt(路径(c)(x))-1)
如果结束
returnPath(CInt(path(c)(x))-1)=迭代_path(path,x+1,path(c)(x))
如果结束
其他的
返回路径(CInt(路径(c)(x))-1)=空
如果结束
下一个
迭代路径=返回路径
端函数
有必要这么复杂吗?你想解决什么问题?您也不能重拨数组的最后一个维度。@Philip A Barnes:是的,很遗憾,这需要非常复杂,但对我来说,这是一个新问题,我无法重拨数组。这是从数据库中的数据为SNMP模拟创建测试数据以检查错误处理的解决方案实际上,您可以Redim
数组的最后一个维度,如果不使用Preserve
,则可以使用任何维度。如果使用Preserve
关键字,如在Redim Preserve
中,则只能重拨最后一个维度。对于语法错误,请尝试Redim PathValues(c,maxDimension2)
@Doug Glancy:这也是多维数组的重拨,但为了节省空间,区分空值和空元素,我还是想研究一下多维数组的使用。
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