Java 增强增强 for 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4308406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java enhanced enhanced for loop
提问by oym
Currently (as of Java 6), using Java's enhanced for-loop I do not know of any way to directly access the iterator index short of reverting to the old indexed for-loop or using an outside counter.
目前(从 Java 6 开始),使用 Java 的增强 for 循环我不知道有什么方法可以直接访问迭代器索引,而不是恢复到旧的索引 for 循环或使用外部计数器。
Are there plans (or perhaps a current way) to implement this "enhanced enhanced" for-loop where one would be able to access the index of the loop (and even possibly manipulate it)?
是否有计划(或者可能是当前的方式)来实现这种“增强型”for 循环,在那里人们可以访问循环的索引(甚至可能操纵它)?
As an example where this might be needed, consider(this is from actual production code):
作为可能需要这样做的示例,请考虑(这是来自实际生产代码):
int i = 1;
for (final String action : actions) {
parameters.set(String.valueOf(i++), action);
}
回答by Yuval Adam
Well, like you said, if you must have an index just use a standard for
loop, or update a counter manually.
好吧,就像你说的,如果你必须有一个索引,就使用标准for
循环,或者手动更新一个计数器。
But rethink your question. Why should you need an index in a for (Object o : list)
loop?
但重新考虑你的问题。为什么在for (Object o : list)
循环中需要索引?
The whole pointof using an enhanced loop is to abstract away the nasty parts of using an index or an iterator.
使用增强循环的全部意义在于抽象掉使用索引或迭代器的讨厌部分。
Most languages today do not give this kind of functionality in loops. Just as an example, the Pythonic way to do this is:
今天的大多数语言都没有在循环中提供这种功能。举个例子,Pythonic 的方法是:
for index, val in enumerate(someList):
# ....
回答by Bozho
No. Use an outside variable.
否。使用外部变量。
回答by Justin Niessner
Why would they need to provide yet another way of accessing elements by index?
为什么他们需要提供另一种通过索引访问元素的方法?
The enhanced for loop was created as a shortcut for when you don't need the index of the element. It streamlined things.
增强的 for 循环是作为不需要元素索引时的快捷方式创建的。它简化了事情。
If you still need the index, fall back to a regular for loop. That's one of the reasons they're there.
如果您仍然需要索引,请回退到常规 for 循环。这就是他们在那里的原因之一。
回答by Emil
It's better to keep an outside counter to get the index,but you could do something like this ( it's bit of an overkill for a simple task):
最好保留一个外部计数器来获取索引,但你可以做这样的事情(对于一个简单的任务来说有点矫枉过正):
class Enum<T> implements Iterable<Indexer<T>> {
private int indx;
private Iterator<T> itr;
private Indexer<T> indxr = new Indexer<T>();
private Enum(Iterable<T> iter) {
this.itr = iter.iterator();
}
public static <T> Enum<T> iterate(Iterable<T> iter) {
return new Enum<T>(iter);
}
@Override
public Iterator<Indexer<T>> iterator() {
return new AbstractIterator<Indexer<T>>() {
@Override
protected Indexer<T> computeNext() {
if (itr.hasNext()) {
indxr.index = indx++;
indxr.val = itr.next();
return indxr;
}
return endOfData();
}
};
}
}
class Indexer<T> {
public int index;
public T val;
@Override
public String toString() {
return String.format("[ %d : %s ]", index, val);
}
For getting index while while iterating:
在迭代时获取索引:
List<Integer> list = new ArrayList<Integer>() ;
list.addAll(Arrays.asList(1, 2, 3, 4, 5,6,55,23,12,24,16));
for (Indexer<Integer> en : Enum.iterate(list))
System.out.println(en.index+" "+en.val);
Note:The above code has dependency to Guava'sAbstractIterator
注意:以上代码依赖Guava 的AbstractIterator
回答by fastcodejava
There isn't way right now unless you keep a counter! I know this is ugly, but java 7 will be better. I don't know why other people are saying we don't need it. Sometimes we need to know what is last member, or odd member, etc.
除非你保留一个柜台,否则现在没有办法!我知道这很难看,但 Java 7 会更好。我不知道为什么其他人说我们不需要它。有时我们需要知道什么是最后一个成员,或奇数成员等。