带数组的 ArrayList [英] ArrayList with Arrays

查看:21
本文介绍了带数组的 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎更复杂"的 ArrayList 没有被广泛使用,因为我找不到任何关于它的具体、有用的信息.

It seems that more "complex" ArrayLists are not widely used, since I'm unable to find any concrete, helpful info about it.

我正在尝试创建一个 ArrayList 的 ArrayList(最终是一个 ArrayList 的 ArrayList of Arrays),但我似乎无法将 Array 添加到 ArrayList 或访问 Array 的元素.所有这些都是在 QTP 中使用 VBScript 完成的.

I'm trying to create an ArrayList of Arrays (and eventually an ArrayList of ArrayLists of Arrays), but I seem unable to either add Arrays to the ArrayList, or access the Array's elements. All this is done using VBScript in QTP.

(代码从 Excel 文件中读取,运行良好.)

(The code reads from an Excel file, which is working fine.)

Set my_sheet = ExcelObject.sheets.item(testCaseSheet)
testCase     = CreateObject("System.Collections.ArrayList")

Function getTestsCaseActions (row, col)
    Do While my_sheet.cells(row, 2).Value <> ""
        MsgBox tempArray(0) & " -> " & tempArray(1) 'WORKS FINE - THE VALUES ARE PRINTED
        testCase.Add tempArray

        row = row+2
    Loop
    End Function

getTestsCaseActions 3, 4

'This is not working - how do I access the arrays and their values in the arraylist?
For Each ArrayItem in testCase
    MsgBox ArrayItem(0)' & ", " & ArrayItem(1)
    'MsgBox "Hey!"
Next

现在,我意识到For Each ArrayItem in testCase 可能是错误的,但我不知道该使用什么?添加到 ArrayList 的元素毕竟是 Arrays.如果我取消注释 MsgBox "Hey!" 行,它只写了一次,即使 ArrayList 应该有 3 个数组.

Now, I realize that For Each ArrayItem in testCase is probably wrong, but I cannot find out what to use? The elements added to the ArrayList are, after all, Arrays. If I uncomment the line MsgBox "Hey!", it's written once, even though the ArrayList should have 3 Arrays.

推荐答案

简短回答:如果您只需要读取访问权限(成功初始化后),则使用 ArrayList Of Arrays 的正确方法:

Short answer: The correct way to use an ArrayList Of Arrays if you just need read access (after a successful initialization):

Option Explicit

Dim alA : Set alA = CreateObject("System.Collections.Arraylist")
alA.add Split("A B C")
alA.add Split("D E F")
alA.add Split("I J K")
WScript.Echo "---- For Each In"
Dim aX
For Each aX In alA
    WScript.Echo TypeName(aX), Join(aX)
Next
WScript.Echo "---- For To"
Dim i
For i = 0 To alA.Count - 1
    WScript.Echo TypeName(alA(i)), Join(alA(i))
Next

输出:

cscript 19915175.vbs
---- For Each In
Variant() A B C
Variant() D E F
Variant() I J K
---- For To
Variant() A B C
Variant() D E F
Variant() I J K

ReDim 保留答案(UBound(answer) + 1):

ArrayList of ArrayLists of Arrays 没有问题(只要我们谈论的是读取访问,并且您不会搞砸):

No problems with an ArrayList Of ArrayLists of Arrays (as long we are talking about read access and you don't mess it up):

Dim alB : Set alB = CreateObject("System.Collections.Arraylist")
alB.Add alA
WScript.Echo "alB(0)(0)(0) =>", alB(0)(0)(0)
WScript.Echo "alB(0)(2)(2) =>", alB(0)(2)(2)

输出:

alB(0)(0)(0) => A
alB(0)(2)(2) => K

这篇关于带数组的 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆