How can I make a copy of an iterator in Java?(如何在 Java 中制作迭代器的副本?)
问题描述
我们有一个元素列表,并且有一个非常简单的碰撞检测,我们检查每个对象与其他对象.
We have a list of elements and have a very simplistic collision detection where we check every object against every other object.
检查是可交换的,所以为了避免重复两次,我们会在 C++ 中这样做:
The check is commutative, so to avoid repeating it twice, we would do this in C++:
for (list<Object>::iterator it0 = list.begin(); it0 != list.end(); ++it0)
{
for (list<Object>::iterator it1 = it0; it1 != list.end(); ++it1)
{
Test(*it0, *it1);
}
}
这里的关键是副本
it1 = it0
你会如何用 Java 写这个?
How would you write this in Java?
推荐答案
你不能复制 Java 迭代器,所以你必须在没有它们的情况下这样做:
You cannot copy Java iterators, so you'll have to do it without them:
for(int i=0; i<list.size(); i++){
for(int j=i; j<list.size(); j++){
Test(list.get(i), list.get(j));
}
}
这篇关于如何在 Java 中制作迭代器的副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Java 中制作迭代器的副本?
基础教程推荐
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 如何对 Java Hashmap 中的值求和 2022-01-01
