Answers:
不可以,但是您可以提供自己的柜台。
这样做的原因是,for-each循环在内部不具有一个计数器; 它基于Iterable接口,即它使用一个Iterator循环遍历“集合”的过程-可能根本不是一个集合,并且实际上可能根本不是基于索引的东西(例如链接列表)。
for(int idx=0, String s; s : stringArray; ++idx) doSomethingWith(s, idx);
                    each_with_index循环方法Enumerable。参见apidock.com/ruby/Enumerable/each_with_index
                    for循环内指定具有初始值的索引变量”,类似于for (int i = 0; String s: stringArray; ++i)
                    还有另一种方式。
假设您编写了自己的Index类和返回Iterable该类的over实例的静态方法,则可以
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迭代该变量的变体)甚至具有索引,甚至具有定义的顺序(某些集合在添加或删除元素时可能会更改顺序)。
例如,请参见以下代码:
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实现而言效率更高。但是,同样在这种情况下,不提供索引。这里的技巧是AtomicInteger在lambda表达式之外使用。注意:在lambda表达式中使用的变量必须有效地为final,这就是为什么我们不能使用normal的原因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();
}尽管提到了许多其他方法可以达到相同的目的,但我还是会与一些不满意的用户分享我的方法。我正在使用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));
});pax的答案有一个“变体” ... ;-)
int i = -1;
for(String s : stringArray) {
    doSomethingWith(s, ++i);
}i=0; i++;方法,使用此方法有什么好处?
                    对于偶尔只需要索引的情况(例如在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(Type t: types) {
  ......
  i++;
}其中Type可以是任何数据类型,而type是要在其上应用循环的变量。
这是我如何执行此操作的示例。这将获得每个循环的索引。希望这可以帮助。
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); 
    }
}
Type var = null; for (var : set) dosomething; if (var != null) then ...