迭代列表是微不足道的.在这种情况下,TCollection
我正在处理的组件的属性.从0
索引迭代到最大索引我没有问题- 我已经做了很多次.
但是,我正在研究一些现在需要以不同方式迭代的东西.我需要从任何给定的起点迭代一个收集项列表 - 然后完成所有项的完整循环.在最后一个列表项之后,它将自动从列表的开头继续迭代.
澄清一下:传统迭代的工作原理如下:
for X := 0 to SomeList.Count-1 do ...
Run Code Online (Sandbox Code Playgroud)
但我可以在其他方面开始,例如:
for X := StartingPoint to EndingPoint do ...
Run Code Online (Sandbox Code Playgroud)
这就是"EndingPoint",我无法弄明白.迭代只会增加.但就我而言,我需要将当前的迭代位置重置为开头 - 正好在迭代的中间.EndingPoint
可能小于StartingPoint
,但它仍然需要做一个完整的循环,一旦它到达结束,它从一开始就开始.
所以,在5个项目的列表中,而不仅仅是...
0, 1, 2, 3, 4
Run Code Online (Sandbox Code Playgroud)
我可以从2开始,想做...
2, 3, 4, 0, 1
Run Code Online (Sandbox Code Playgroud)
我如何完成这样的循环?
Rob*_*edy 15
for foo := 0 to Pred(SomeList.Count) do begin
i := (foo + StartingPoint) mod SomeList.Count;
...
end;
Run Code Online (Sandbox Code Playgroud)
使用i
循环内的索引; 忽略foo
变量.
从范围的中间到结尾,i
将是平等的foo + StartingPoint
.之后,mod
操作员将有效地将i
"环绕"重新开始.