我在Excel中使用vba,我希望循环遍历数组,但我希望只循环一次组合。我正在循环遍历所有排列,这会导致重复组合。例如:
For i = 1 to 4
'execute some code
For k = 1 to 4
'execute some code
Next
Next
这将给出i和j的以下组合:
[1,1] dont want as 1 is repeated twice
[1,2] yes please
[1,3] yes please
[1,4] yes please
[2,1] dont want as [1,2] was already given, the order doesnt matter
[2,2] dont want as 2 is repeated twice
[2,3] yes please
[2,4] yes please
etc.
抓住我的漂移?以下文章也讨论了它,但不幸的是在Java中,我无法理解它背后的逻辑来适应Excel中的vba:loop through combination once。我通过仅使用两个变量i和j来简化我的解释,其中实际上它是更多的组合(最多8个For循环)并且它们从大约100到900循环,这需要极长的处理时间来完成所有排列。因此希望能够了解如何仅通过独特的组合,这将显着缩短处理时间。
非常感谢任何帮助
答案 0 :(得分:2)
Java代码所做的是从外部循环到达的内部循环开始,+ 1
For i = 1 to 4
'execute some code
For k = i + 1 to 4
Debug.Print i, k
'execute some code
Next
Next
产生
1 2
1 3
1 4
2 3
2 4
3 4