- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在阅读 Effective Java
Item 46: Prefer for-each loops to traditional for loops
在提到需要迭代器/for循环而不是for-each循环的情况的部分,有一点:
Parallel iteration—If you need to traverse multiple collections in parallel, then you need explicit control over the iterator or index variable, so that all iterators or index variables can be advanced in lockstep.
现在,我明白了对迭代器/索引变量的显式控制意味着什么(不是每个循环的 Controller )。但是我无法理解这个意义上的lockstep的含义。我试着用谷歌搜索它并找到了一篇关于 Wikipedia 的文章其中指出:
Lockstep systems are fault-tolerant computer systems that run the same set of operations at the same time in parallel.
这我理解为具有例如用于故障转移的服务器的附加实例没关系。但是我无法完全理解在编程中迭代集合的上下文中的确切含义。
最佳答案
在这个语境下,意思更像是military marching .
或者,当一个操作前进时,其他操作也随之前进/跟随。
或者更具体地说,如果你想遍历两个集合,你不能轻易地使用 foreach 结构:
for (Item i : list1) { //only allows you to iterate over 1 list.
}
遍历 2 个集合 )
Iterator iter1 = list1.iterator();
Iterator iter2 = list2.iterator();
while (iter1.hasNext() && iter2.hasNext()){
Item a = iter1.next();
Item b = iter2.next();
doSomething(a, b);
}
即在迭代 list1 时,迭代 list2 随之而来——“步调一致”
关于java - Lockstep在编程中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22260322/
复制 http://forum.dlang.org/thread/arlokcqodltcazdqqlby@forum.dlang.org比较回答速度:) 我基本上希望能够做这样的事情: auto r
我是一名优秀的程序员,十分优秀!