【发布时间】:2018-05-14 12:05:11
【问题描述】:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
上面的代码将依次迭代 1 到 6。我们可以交替迭代同一个列表,以便在不更改 while 循环的情况下打印1, 3, 5?
【问题讨论】:
-
一种可能性是重新排列您的列表,或者在打印之前检查值 (
if (x==1 || x==3 || x==6))。 (它没有回答你的问题,这似乎是不可能的) -
只是想知道:你为什么要问?你想在这里解决什么真正的问题?