I'm currently writing a code and want to end my iterator at a certain index and I'm not able to figure it out.' This is my code but doesn't work.
我正在编写一个代码,想要在某个索引处结束我的迭代器,我无法弄明白。这是我的代码,但不起作用。
for(int i = 0; i< stop && iterator.hasNext(); i++){
iterator.next();
if(i == stop){
break;
}
return iterator;
}
return iterator;
3 个解决方案
#1
1
Just remove return iterator from inside the loop and your code will work fine. Right now, it is returning from your function on the loop's first iteration itself, i.e. when i = 0.
只需从循环内部删除return迭代器,您的代码就可以正常工作。现在,它是从循环的第一次迭代本身的函数返回,即当i = 0时。
#2
0
You just need to break when you reach the position you want.
当你到达你想要的位置时,你只需要打破。
public static void main(String[] args) {
List<String> xs = Arrays.asList("A","B","C","D","E","F");
Iterator<String> iter = xs.iterator();
offsetIter(iter,3);
while (iter.hasNext()) {
String string = (String) iter.next();
System.out.print(" ");
System.out.print(string);
}
}
public static <T> void offsetIter(Iterator<T> iter , int moveAt){
int i = 0;
while (iter.hasNext()) {
if(i<moveAt){
iter.next();
i++;
} else {
break;
}
}
return;
}
Will result in:
将导致:
D E F
No need to return an iterator since iterator are mutable by principle.
因为迭代器原则上是可变的,所以不需要返回迭代器。
#3
0
If we look at your code, there are a couple of things to note. First of all, it will always return with i == 0.
You first time through the loop if i > 0 and it has next, you grab next and return iterator. Otherwise the loop doesn't run and you return iterator. As a note, the if can never be true because you will not run the loop in that case.
如果我们查看您的代码,有几点需要注意。首先,它将始终以i == 0返回。如果i> 0并且接下来,则第一次通过循环,然后抓住next并返回迭代器。否则循环不会运行并返回迭代器。作为一个注释,if永远不会是真的,因为在那种情况下你不会运行循环。
The code as written is equivalent to:
编写的代码相当于:
if ((i < stop) && iterator.hasNext()) {
iterator.next();
}
return iterator;
#1
1
Just remove return iterator from inside the loop and your code will work fine. Right now, it is returning from your function on the loop's first iteration itself, i.e. when i = 0.
只需从循环内部删除return迭代器,您的代码就可以正常工作。现在,它是从循环的第一次迭代本身的函数返回,即当i = 0时。
#2
0
You just need to break when you reach the position you want.
当你到达你想要的位置时,你只需要打破。
public static void main(String[] args) {
List<String> xs = Arrays.asList("A","B","C","D","E","F");
Iterator<String> iter = xs.iterator();
offsetIter(iter,3);
while (iter.hasNext()) {
String string = (String) iter.next();
System.out.print(" ");
System.out.print(string);
}
}
public static <T> void offsetIter(Iterator<T> iter , int moveAt){
int i = 0;
while (iter.hasNext()) {
if(i<moveAt){
iter.next();
i++;
} else {
break;
}
}
return;
}
Will result in:
将导致:
D E F
No need to return an iterator since iterator are mutable by principle.
因为迭代器原则上是可变的,所以不需要返回迭代器。
#3
0
If we look at your code, there are a couple of things to note. First of all, it will always return with i == 0.
You first time through the loop if i > 0 and it has next, you grab next and return iterator. Otherwise the loop doesn't run and you return iterator. As a note, the if can never be true because you will not run the loop in that case.
如果我们查看您的代码,有几点需要注意。首先,它将始终以i == 0返回。如果i> 0并且接下来,则第一次通过循环,然后抓住next并返回迭代器。否则循环不会运行并返回迭代器。作为一个注释,if永远不会是真的,因为在那种情况下你不会运行循环。
The code as written is equivalent to:
编写的代码相当于:
if ((i < stop) && iterator.hasNext()) {
iterator.next();
}
return iterator;