[英]Is there a way to access an iteration-counter in Java's for-each loop?
Java的for-each循環有沒有辦法
for(String s : stringArray) {
doSomethingWith(s);
}
找出循環處理的頻率?
除了使用舊的和眾所周知的for(int i=0; i < boundary; i++)
- 循環之外,是構造
int i = 0;
for(String s : stringArray) {
doSomethingWith(s);
i++;
}
在 for-each 循環中提供這樣一個計數器的唯一方法是什么?
不可以,但您可以提供自己的櫃台。
這樣做的原因是,for-each循環內部沒有一個計數器; 它基於Iterable接口,即它使用Iterator
循環遍歷“集合”——它可能根本不是一個集合,實際上可能根本不是基於索引的東西(例如鏈表)。
還有另一種方法。
鑒於您編寫自己的Index
類和一個靜態方法,該Iterable
在此類的實例上返回一個Iterable
,您可以
for (Index<String> each: With.index(stringArray)) {
each.value;
each.index;
...
}
With.index
的實現類似於
class With {
public static <T> Iterable<Index<T>> index(final T[] array) {
return new Iterable<Index<T>>() {
public Iterator<Index<T>> iterator() {
return new Iterator<Index<T>>() {
index = 0;
public boolean hasNext() { return index < array.size }
public Index<T> next() { return new Index(array[index], index++); }
...
}
}
}
}
}
最簡單的解決方案是運行您自己的計數器:
int i = 0;
for (String s : stringArray) {
doSomethingWith(s, i);
i++;
}
這樣做的原因是因為沒有實際保證集合中的項目( for
變體對其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
表明,正確地,順序不被視為集合的顯着特征。
在沒有手動計數器的情況下,還有其他方法可以做到這一點,但為了可疑的利益,這是一項相當大的工作。
在Java 8 中使用lambda和函數式接口使得創建新的循環抽象成為可能。 我可以使用索引和集合大小遍歷集合:
List<String> strings = Arrays.asList("one", "two","three","four");
forEach(strings, (x, i, n) -> System.out.println("" + (i+1) + "/"+n+": " + x));
哪些輸出:
1/4: one
2/4: two
3/4: three
4/4: four
我實現為:
@FunctionalInterface
public interface LoopWithIndexAndSizeConsumer<T> {
void accept(T t, int i, int n);
}
public static <T> void forEach(Collection<T> collection,
LoopWithIndexAndSizeConsumer<T> consumer) {
int index = 0;
for (T object : collection){
consumer.accept(object, index++, collection.size());
}
}
可能性是無止境。 例如,我創建了一個抽象,它只為第一個元素使用一個特殊的函數:
forEachHeadTail(strings,
(head) -> System.out.print(head),
(tail) -> System.out.print(","+tail));
正確打印逗號分隔列表:
one,two,three,four
我實現為:
public static <T> void forEachHeadTail(Collection<T> collection,
Consumer<T> headFunc,
Consumer<T> tailFunc) {
int index = 0;
for (T object : collection){
if (index++ == 0){
headFunc.accept(object);
}
else{
tailFunc.accept(object);
}
}
}
圖書館會開始出現來做這些事情,或者你可以自己動手。
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
});
恐怕這對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接口允許您訪問it.nextIndex()
。
(編輯)
對於您更改的示例:
for(ListIterator<String> it =l.listIterator(); it.hasNext() ;)
{
int i = it.nextIndex();
doSomethingWith(it.next(), i);
}
final Set<Double> doubles; // boilerplate
final Iterator<Double> iterator = doubles.iterator();
for (int ordinal = 0; iterator.hasNext(); ordinal++)
{
System.out.printf("%d:%f",ordinal,iterator.next());
System.out.println();
}
這實際上是谷歌在Guava 討論中建議的解決方案,關於為什么他們沒有提供
CountingIterator
。
Sun
正在考慮對Java7
進行的更改Java7
是在 foreach 循環中提供對內部Iterator
訪問。 語法將是這樣的(如果接受的話):
for (String str : list : it) {
if (str.length() > 100) {
it.remove();
}
}
這是語法糖,但顯然很多人都對這個功能提出了要求。 但是在獲得批准之前,您必須自己計算迭代次數,或者使用帶有Iterator
的常規 for 循環。
雖然提到了很多其他方法來實現相同的目標,但我會為一些不滿意的用戶分享我的方法。 我正在使用 Java 8 IntStream 功能。
1. 數組
Object[] obj = {1,2,3,4,5,6,7};
IntStream.range(0, obj.length).forEach(index-> {
System.out.println("index: " + index);
System.out.println("value: " + obj[index]);
});
2. 列表
List<String> strings = new ArrayList<String>();
Collections.addAll(strings,"A","B","C","D");
IntStream.range(0, strings.size()).forEach(index-> {
System.out.println("index: " + index);
System.out.println("value: " + strings.get(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);
}
}
如果在 for-each 循環中需要計數器,則必須自己計算。 據我所知,沒有內置櫃台。
pax的答案有一個“變體”...... ;-)
int i = -1;
for(String s : stringArray) {
doSomethingWith(s, ++i);
}
int i = 0;
for(String s : stringArray) {
doSomethingWith(s,i);
++i;
}
方法有很多,但方法是其中一種
我發現使用帶有遞增索引的簡單for循環是最有效,最可靠的解決方案,如下所示: https : //stackoverflow.com/a/3431543/2430549
for (Object child : mChildren) {
int index = mChildren.indexOf(child);
}
這是獲取 foreach 中每個對象的索引的最簡單方法。
我通常使用數組來解決這個問題
List<String> list = new ArrayList<String>();
final int[] counter = new int[1];
list.foreach( item -> {
list.get(counter[0]) // code here that can use counter[0] for counter value
counter[0]++; // increment the counter
}
一個計數器很酷,但是更簡單的方法是:
Stream.of(yourArray).forEach(i -> Arrays.asList(yourArray).indexOf(i));
快速演示:
import java.io.PrintStream;
import java.util.Arrays;
import java.util.stream.*;
class Main {
public static void main(String[] args) {
PrintStream p = System.out;
String[] arr = {"Abraham", "James", "Donald", "Max"};
Stream.of(arr).forEach(i -> p.println(Arrays.asList(arr).indexOf(i) + " : " + i));
}
}
親自查看它: https : //repl.it/@abranhe/forEachIndex
最好和優化的解決方案是做以下事情:
int i=0;
for(Type t: types) {
......
i++;
}
其中 Type 可以是任何數據類型, types 是您在其上應用循環的變量。
我有點驚訝沒有人提出以下建議(我承認這是一種懶惰的方法......); 如果 stringArray 是某種列表,您可以使用類似 stringArray.indexOf(S) 之類的東西來返回當前計數的值。
注意:這假設 List 的元素是唯一的,或者它們是否不唯一都沒有關系(因為在這種情況下它將返回找到的第一個副本的索引)。
在某些情況下,這就足夠了......
這是我如何做到這一點的一個例子。 這將獲取每個循環的索引。 希望這可以幫助。
public class CheckForEachLoop {
public static void main(String[] args) {
String[] months = new String[] { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST",
"SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" };
for (String s : months) {
if (s == months[2]) { // location where you can change
doSomethingWith(s); // however many times s and months
// doSomethingWith(s) will be completed and
// added together instead of counter
}
}
System.out.println(s);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.