我的代码:
Object tempDifference;
Object testUser;
testUser = users.get(0);
it8 = difference_time.iterator();
it9 = users.iterator();
Object tempUser;
tempDifference = difference_time.get(0);
while (it9.hasNext()) {
tempUser = it9.next();
System.out.println(testUser+" "+tempUser);
if (testUser.equals(tempUser)) {
times.add(tempDifference);
testUser = tempUser;
} else {
times.add(value);
testUser = tempUser;
}
tempDifference = it8.next();
}
输入为:1 2 2
输出为:1 1
我想要输出1 2
为什么开始时输出错误1 1
?
答案 0 :(得分:1)
大多数迭代器在第一个元素之前启动,因此第一次调用next()会将它们移动到第一个元素,而不是第二个元素。
所以,如果用户= {1,2},并将其煮熟一点:
//set test = 1, the first entry in the list
test = users.get(0);
//get a new iterator, starting before the first element
it9 = users.iterator();
// move to first element (the next one), so temp == 1
temp = it9.next();
// print values "1 1"
System.out.println(test+" "+temp);