314

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 循环中使用这种计数器的唯一方法是什么?

4

16 回答 16

234

不可以,但您可以提供自己的柜台。

原因是for-each循环内部没有计数器;它基于Iterable接口,即它使用 anIterator循环遍历“集合”——它可能根本不是一个集合,实际上可能根本不是基于索引的东西(例如链表)。

于 2009-01-25T11:36:35.700 回答
73

还有另一种方法。

鉴于您编写了自己的Index类和一个返回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++); }
                    ...
                }
            }
        }
    }
}
于 2009-09-29T08:45:30.327 回答
61

最简单的解决方案运行您自己的计数器:

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

表明,正确地,顺序不被认为是集合的显着特征。

还有其他方法可以在没有手动计数器的情况下做到这一点,但这是一项相当可观的工作,但效果却很可疑。

于 2009-01-25T11:18:48.967 回答
28

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);
      }
   }
}

图书馆将开始弹出来做这些事情,或者你可以自己动手。

于 2014-04-05T13:22:47.380 回答
28

Java 8 引入了Iterable#forEach()/方法,与“经典” for-each 循环相比,它对许多/实现Map#forEach()更有效。然而,在这种情况下,也没有提供索引。这里的技巧是在 lambda 表达式之外使用。注意:在 lambda 表达式中使用的变量必须是有效的 final,这就是为什么我们不能使用普通的.CollectionMapAtomicIntegerint

final AtomicInteger indexHolder = new AtomicInteger();
map.forEach((k, v) -> {
    final int index = indexHolder.getAndIncrement();
    // use the index
});
于 2016-10-18T19:03:53.807 回答
17

恐怕这是不可能的foreach。但我可以建议你一个简单的老式 for-loops

    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);
    }
于 2009-01-25T11:54:13.870 回答
11

Sun正在考虑的更改之一Java7是提供对Iteratorforeach 循环内部的访问。语法将是这样的(如果这被接受):

for (String str : list : it) {
  if (str.length() > 100) {
    it.remove();
  }
}

这是语法糖,但显然很多人为此功能提出了要求。但在获得批准之前,您必须自己计算迭代次数,或者使用带有Iterator.

于 2009-01-25T12:43:29.153 回答
11

惯用的解决方案:

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();
}

这实际上是 Google 在Guava 讨论中建议的解决方案,说明他们为什么不提供CountingIterator.

于 2017-10-13T03:25:32.420 回答
10

尽管提到了很多其他方法可以实现相同的目标,但我会为一些不满意的用户分享我的方法。我正在使用 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));
});
于 2019-01-10T05:57:50.543 回答
3

对于我偶尔只需要索引的情况,比如在 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);
  }
}
于 2017-06-14T17:23:05.840 回答
2

如果你需要一个 for-each 循环中的计数器,你必须自己计算。据我所知,没有内置计数器。

于 2009-01-25T11:13:12.377 回答
1

pax 的答案有一个“变体”...... ;-)

int i = -1;
for(String s : stringArray) {
    doSomethingWith(s, ++i);
}
于 2010-06-30T13:13:29.833 回答
0
for (Object child : mChildren) {
  int index = mChildren.indexOf(child);
}

这是获取 foreach 中每个对象的索引的最简单方法。

于 2022-02-01T11:00:00.537 回答
-1

最佳和优化的解决方案是执行以下操作:

int i=0;

for(Type t: types) {
  ......
  i++;
}

其中 Type 可以是任何数据类型,types 是您申请循环的变量。

于 2020-01-04T20:55:04.340 回答
-4

我有点惊讶没有人提出以下建议(我承认这是一种懒惰的方法......);如果 stringArray 是某种列表,您可以使用类似 stringArray.indexOf(S) 的东西来返回当前计数的值。

注意:这假定 List 的元素是唯一的,或者它们是否非唯一无关紧要(因为在这种情况下它将返回找到的第一个副本的索引)。

在某些情况下,这就足够了……

于 2011-03-04T23:02:27.683 回答
-5

这是我如何做到这一点的一个例子。这将在 for each 循环中获取索引。希望这可以帮助。

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); 


    }
}
于 2019-02-18T23:06:38.890 回答