在Java的for-each循环中有办法吗
for(String s : stringArray) {
doSomethingWith(s);
}
找出循环已经被处理的频率?
除了使用老的和众所周知的for(int i=0;I <边界;i++) - loop,是构造
int i = 0;
for(String s : stringArray) {
doSomethingWith(s);
i++;
}
在for-each循环中有这样一个计数器的唯一方法是什么?
在Java的for-each循环中有办法吗
for(String s : stringArray) {
doSomethingWith(s);
}
找出循环已经被处理的频率?
除了使用老的和众所周知的for(int i=0;I <边界;i++) - loop,是构造
int i = 0;
for(String s : stringArray) {
doSomethingWith(s);
i++;
}
在for-each循环中有这样一个计数器的唯一方法是什么?
当前回答
恐怕foreach不行。但我可以给你一个简单的老式for循环:
List<String> l = new ArrayList<String>();
l.add("a");
l.add("b");
l.add("c");
l.add("d");
// the array
String[] array = new String[l.size()];
for(ListIterator<String> it =l.listIterator(); it.hasNext() ;)
{
array[it.nextIndex()] = it.next();
}
注意,List接口允许您访问它。nextindex()。
(编辑)
到你改变的例子:
for(ListIterator<String> it =l.listIterator(); it.hasNext() ;)
{
int i = it.nextIndex();
doSomethingWith(it.next(), i);
}
其他回答
最佳和优化的解决方案是做以下事情:
int i=0;
for(Type t: types) {
......
i++;
}
其中Type可以是任何数据类型,types是应用于循环的变量。
Java 8引入了Iterable#forEach() / Map#forEach()方法,与“经典的”for-each循环相比,该方法对于许多Collection / Map实现更有效。但是,在这种情况下也不提供索引。这里的技巧是在lambda表达式之外使用AtomicInteger。注意:lambda表达式中使用的变量必须有效为final,这就是为什么我们不能使用普通int类型。
final AtomicInteger indexHolder = new AtomicInteger();
map.forEach((k, v) -> {
final int index = indexHolder.getAndIncrement();
// use the index
});
对于偶尔需要索引的情况,比如在catch子句中,我有时会使用indexOf。
for(String s : stringArray) {
try {
doSomethingWith(s);
} catch (Exception e) {
LOGGER.warn("Had some kind of problem with string " +
stringArray.indexOf(s) + ": " + s, e);
}
}
最简单的解决方法是运行你自己的计数器:
int i = 0;
for (String s : stringArray) {
doSomethingWith(s, i);
i++;
}
这样做的原因是,实际上并不能保证集合(for的变体迭代遍历)中的项具有索引,甚至具有定义的顺序(当您添加或删除元素时,一些集合可能会改变顺序)。
例如,请参阅以下代码:
import java.util.*;
public class TestApp {
public static void AddAndDump(AbstractSet<String> set, String str) {
System.out.println("Adding [" + str + "]");
set.add(str);
int i = 0;
for(String s : set) {
System.out.println(" " + i + ": " + s);
i++;
}
}
public static void main(String[] args) {
AbstractSet<String> coll = new HashSet<String>();
AddAndDump(coll, "Hello");
AddAndDump(coll, "My");
AddAndDump(coll, "Name");
AddAndDump(coll, "Is");
AddAndDump(coll, "Pax");
}
}
当你运行它时,你可以看到类似的东西:
Adding [Hello]
0: Hello
Adding [My]
0: Hello
1: My
Adding [Name]
0: Hello
1: My
2: Name
Adding [Is]
0: Hello
1: Is
2: My
3: Name
Adding [Pax]
0: Hello
1: Pax
2: Is
3: My
4: Name
这表明,顺序不被认为是一个集合的显著特征。
除了手动计数器,还有其他方法可以做到这一点,但这是一项相当大的工作,而且效益不确定。
没有,但你可以提供自己的柜台。
这样做的原因是for-each循环内部没有计数器;它基于Iterable接口,也就是说,它使用一个迭代器来遍历“集合”——这可能根本不是一个集合,实际上可能根本不是基于索引的东西(比如链表)。