ArrayList的使用数组 [英] ArrayList with Arrays

查看:165
本文介绍了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(最终阵列的ArrayLists的ArrayList),但我似乎无法要么阵列添加到ArrayList,或访问数组元素。所有这一切都是在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.

(将code从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

现在,我意识到对于每个ArrayItem在测试用例可能是错的,但我不能找出使用什么?添加到ArrayList的元素,毕竟阵列。如果我取消注释行 MSGBOX嘿!,它的写一次,即使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阵列,如果你只需要读访问权(初始化成功后):

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 preserve答案(UBound函数(回答)+ 1):

没有问题,一个ArrayList阵列的ArrayList中(只要我们正在谈论的读取权限,你不惹它):

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天全站免登陆