1

请找到以下代码:

Option Explicit
Dim TempArr
Dim ArrListChildDetails :  Set ArrListChildDetails = CreateObject("System.Collections.ArrayList")

现在我已经为它分配了一些 Range 对象:

ArrListChildDetails = ob3.Cells(ParentMatchRowNum,Width+1).Resize(, UBound(TempArr, 1) + 1)
TempArr=ArrListChildDetails.ToArray()   
ArrListChildDetails .Sort();

现在是否可以比较两个数组,如果它们的元素相同且位置相同并且计数也一样?意味着IsEql()ArrayList 类中的任何可用?

4

2 回答 2

1

我不知道你从哪里得到 IsEqual(),但你可以使用 ArrayList/Object 方法 .Equals() 来确定一个 ArrayList 是否被另一个名称/变量引用(参见 VBScript 的 Is 运算符)。

要检查两个不同 ArrayList 的计数、顺序和元素是否相等,您必须滚动自己的函数。为了让你开始:

Option Explicit

Dim alA  : Set alA  = CreateObject("System.Collections.ArrayList")
Dim alB  : Set alB  = CreateObject("System.Collections.ArrayList")
Dim alC  : Set alC  = CreateObject("System.Collections.ArrayList")
Dim alA2 : Set alA2 = alA

alA.Add "one"
alA.Add "two"

alB.Add "one"
alB.Add "two"

alC.Add "one"
alC.Add "owt"

WScript.Echo "alA: ", Join(AlA.ToArray())
WScript.Echo "alA2:", Join(AlA2.ToArray())
WScript.Echo "alB: ", Join(AlB.ToArray())
WScript.Echo "alC: ", Join(AlC.ToArray())

WScript.Echo "alA.Equals(alA2):", CStr(alA.Equals(alA2))
WScript.Echo "alA.Equals(alB):" , CStr(alA.Equals(alB))
WScript.Echo "alA.Equals(alC):" , CStr(alA.Equals(alC))

WScript.Echo "ALEqual(alA, alA):", CStr(ALEqual(alA, alA))
WScript.Echo "ALEqual(alA, alA2):", CStr(ALEqual(alA, alA2))
WScript.Echo "ALEqual(alA, alB):", CStr(ALEqual(alA, alB))
WScript.Echo "ALEqual(alA, alC):", CStr(ALEqual(alA, alC))

Function ALEqual(alL, alR)
  ALEqual = True
  If alL Is AlR Then Exit Function
  ALEqual = alL.Count = alR.Count
  If ALEqual Then
     Dim i
     For i = 0 To alL.Count - 1
         If alL(i) <> alR(i) Then
            ALEqual = False
            Exit Function
         End If   
     Next
  End If  
End Function

输出:

alA:  one two
alA2: one two
alB:  one two
alC:  one owt
alA.Equals(alA2): Wahr
alA.Equals(alB): Falsch
alA.Equals(alC): Falsch
ALEqual(alA, alA): Wahr
ALEqual(alA, alA2): Wahr
ALEqual(alA, alB): Wahr
ALEqual(alA, alC): Falsch
于 2012-12-24T10:01:58.210 回答
1

快速而肮脏的解决方案,但在大多数情况下就足够了:

WScript.Echo Join(ArrListChildDetails.ToArray(),"|") = Join(ArrListChildDetails2.ToArray(),"|")

编辑:一些解释:比较两个 ArrayLists(或两个数组),转换为一个数组并将它们连接到一个字符串中,该字符串由数组中未使用的一些特殊字符分隔,然后比较字符串,当然这仅适用于一个具有可以字符串化的值类型的维数组。如果它们具有相同数量的具有相同内容的元素,则比较将返回 -1(真)和 0(假),否则

于 2012-12-24T10:50:58.960 回答