gpt4 book ai didi

arrays - 如何在 VBA 中设置 "jagged array"?

转载 作者:行者123 更新时间:2023-12-03 11:48:20 26 4
gpt4 key购买 nike

我的教室里挤满了 child ,每个人都必须列出他们最喜欢的玩具以进行作业。有些 child 只列出 1 个玩具,而其他 child 列出更多。

我如何创建一个锯齿状的数组,这样 Kids(x)(y)... 其中 x 是我类 child 的数量,y 是他们列为他们最喜欢的玩具列表?

最佳答案

“锯齿状数组”是数组数组的俚语。 VBA Variant数据类型可以包含几乎任何东西*,包括数组。所以你创建了一个 Variant 类型的数组,并为其每个元素分配一个任意长度的数组(即并非所有元素都必须具有相同的长度)。

下面是一个例子:

Dim nStudents As Long
Dim iStudent As Long
Dim toys() As Variant
Dim nToys As Long
Dim thisStudentsToys() As Variant

nStudents = 5 ' or whatever

ReDim toys(1 To nStudents) ' this will be your jagged array

For iStudent = 1 To nStudents
'give a random number of toys to this student (e.g. up to 10)
nToys = Int((10 * Rnd) + 1)
ReDim thisStudentsToys(1 To nToys)

'code goes here to fill thisStudentsToys()
'with their actual toys

toys(iStudent) = thisStudentsToys
Next iStudent

' toys array is now jagged.

' To get student #3's toy #7:
MsgBox toys(3)(7)
'will throw an error if student #3 has less than 7 toys

* 一个值得注意的异常(exception)是用户定义的类型。变体不能包含这些。

关于arrays - 如何在 VBA 中设置 "jagged array"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9435608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com