1

我有一个数组说:VMHArray=(12,22,34,4)现在有另一个Arraylist对象说ArrayListTaskDetails将数据保存为(12,55,44,4,12,22,21,107,43,22,34)现在下面的代码我写了从列表中删除ArrayListTaskDetails不存在于VMHArray.

代码

Dim Flag : Flag = true
Dim Counter
For IndexSearch = 0 To ArrayListTaskDetails.Count - 1 step 4

    Counter = 0
    Do while Flag

        If VMHArray(Counter) <> ArrayListTaskDetails (IndexSearch) Then

          ArrayListTaskDetails.RemoveRange IndexSearch, 4
          Flag = False

        End If
    Counter = Counter + 1
    Loop

Next

现在假设在 处找到匹配项IndexSearch = 0,因此它将删除位置处的元素0,1,2,3- 非常好。但是由于移除和使ArrayList对象按功能连续,其他元素将4向左移动。现在问题For Loop已经增加了 4,所以它将从下一次迭代开始location 4。因此,现在0 location数组列表中的新元素永远没有机会测试与VMHArray数组元素的相等性。我们是否可以在不丢失预期操作的情况下处理这种情况,或者无论如何我们可以将For Loop计数器设置为其直接位置,在该位置找到最近的匹配并删除了 4 个元素。?

如果有任何问题了解问题,请告诉我!

编辑 Go to将不起作用

代码(根据@Ankit 的建议)

Option Explicit

Dim Flag : Flag = true
Dim Counter
Dim VMHArray : VMHArray = Array(12,22,34,4) 
Dim ArrayListTaskDetails : Set ArrayListTaskDetails = CreateObject("System.Collections.ArrayList")

    ArrayListTaskDetails.Add(45)
    ArrayListTaskDetails.Add(4)
    ArrayListTaskDetails.Add(22)
    ArrayListTaskDetails.Add(4)
    ArrayListTaskDetails.Add(45)
    ArrayListTaskDetails.Add(20)
    ArrayListTaskDetails.Add(12)
    ArrayListTaskDetails.Add(35)
    ArrayListTaskDetails.Add(34)

    Restart:
For IndexSearch = 0 To ArrayListTaskDetails.Count - 1 step 4

    Counter = 0
    Do while Flag

        If VMHArray(Counter) <> ArrayListTaskDetails (IndexSearch) Then

          ArrayListTaskDetails.RemoveRange IndexSearch, 4
          Goto Restart
          Flag = False

        End If
    Counter = Counter + 1
    Loop

Next

MsgBox(Join(ArrayListTaskDetails.ToArray()),"$")

错误

谢谢,

4

2 回答 2

5

解决问题的两个思路:

  1. 使用字典检查要保留的项目
  2. 向后循环

在代码中:

Option Explicit

Dim alSrc : Set alSrc = CreateObject("System.Collections.ArrayList")
Dim sE
For Each sE In Split("a x x x b x x x c x x x d x x x e x x x f x x x a x x x")
    alSrc.Add sE
Next
WScript.Echo "alSrc:", Join(alSrc.ToArray(), ".")
Dim aKeep   : aKeep       = Split("a d")
WScript.Echo "aKeep:", Join(aKeep, ".")
Dim dicKeep : Set dicKeep = CreateObject("Scripting.Dictionary")
For Each sE In aKeep
    dicKeep(sE) = 0
Next
WScript.Echo "dicKeep:", Join(dicKeep.Keys(), ".")
Dim nI
For nI = alSrc.Count - 4 To 0 Step -4
    sE = alSrc(nI)
    If Not dicKeep.Exists(sE) Then
       alSrc.RemoveRange nI, 4
    End If
    WScript.Echo sE, nI, Join(alSrc.ToArray(), ".")
Next
WScript.Echo "alSrc:", Join(alSrc.ToArray(), ".")

输出:

alSrc: a.x.x.x.b.x.x.x.c.x.x.x.d.x.x.x.e.x.x.x.f.x.x.x.a.x.x.x
aKeep: a.d
dicKeep: a.d
a 24 a.x.x.x.b.x.x.x.c.x.x.x.d.x.x.x.e.x.x.x.f.x.x.x.a.x.x.x
f 20 a.x.x.x.b.x.x.x.c.x.x.x.d.x.x.x.e.x.x.x.a.x.x.x
e 16 a.x.x.x.b.x.x.x.c.x.x.x.d.x.x.x.a.x.x.x
d 12 a.x.x.x.b.x.x.x.c.x.x.x.d.x.x.x.a.x.x.x
c 8 a.x.x.x.b.x.x.x.d.x.x.x.a.x.x.x
b 4 a.x.x.x.d.x.x.x.a.x.x.x
a 0 a.x.x.x.d.x.x.x.a.x.x.x
alSrc: a.x.x.x.d.x.x.x.a.x.x.x

至于 .Count - ? 的奥秘:

1 2 3 4 5 6 7 8 - Count 1 - 8
0 1 2 3 4 5 6 7 - Idx   0 - 7
A x x x B x x x - Count (8) - Step (4) = Pos to check (4)
                  Count (8) - Step + 1 (5) = Not the pos to check (3)
于 2013-01-02T14:18:40.657 回答
0

重置For loop counter意味着你有点不让它iterate。您必须非常小心,以免遇到infinite loop. 坦率地说,没有阅读你的大文本。但是查看您的代码和标题,请尝试以下操作:

For IndexSearch = 0 To ArrayListTaskDetails.Count - 1 step 4
    Counter = 0
    Do while Flag
       If VMHArray(Counter) = ArrayListTaskDetails (IndexSearch) Then
          ArrayListTaskDetails.RemoveRange IndexSearch, 4
          Flag = False
          If IndexSearch >= 4 then 
            IndexSearch = IndexSearch - 4 '-- add this to go back to previous index
          End If
       End If
       Counter = Counter + 1
    Loop
Next IndexSearch
于 2013-01-02T14:04:04.543 回答