Java,如何在“for each”循环中获取当前索引/键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3431529/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:43:49  来源:igfitidea点击:

Java, How do I get current index/key in "for each" loop

java

提问by droidgren

In Java, How do I get the current index for the element in Java?

在 Java 中,如何获取 Java 中元素的当前索引?

for (Element song: question){
    song.currentIndex();         //<<want the current index.
}

In PHP you could do this:

在 PHP 中,你可以这样做:

foreach ($arr as $index => $value) {
    echo "Key: $index; Value: $value";
}

采纳答案by Michael Mrozek

You can't, you either need to keep the index separately:

你不能,你要么需要单独保留索引:

int index = 0;
for(Element song : question) {
    System.out.println("Current index is: " + (index++));
}

or use a normal for loop:

或使用正常的 for 循环:

for(int i = 0; i < question.length; i++) {
    System.out.println("Current index is: " + i);
}

The reason is you can use the condensed for syntax to loop over any Iterable, and it's not guaranteed that the values actually have an "index"

原因是您可以使用精简的 for 语法循环遍历任何Iterable,并且不能保证这些值实际上具有“索引”

回答by TheLQ

In Java, you can't, as foreach was meant to hide the iterator. You must do the normal For loop in order to get the current iteration.

在 Java 中,您不能,因为 foreach 旨在隐藏迭代器。您必须执行正常的 For 循环才能获得当前迭代。

回答by Saher Ahwal

Keep track of your index: That's how it is done in Java:

跟踪您的索引:这就是在 Java 中完成的方式:

 int index = 0;
    for (Element song: question){
        // Do whatever
         index++;
    }

回答by Zeffer

Not possible in Java.

在 Java 中不可能。



Here's the Scala way:

这是 Scala 的方式:

val m = List(5, 4, 2, 89)

for((el, i) <- m.zipWithIndex)
  println(el +" "+ i)

回答by likejudo

As others pointed out, 'not possible directly'. I am guessing that you want some kind of index key for Song? Just create another field (a member variable) in Element. Increment it when you add Song to the collection.

正如其他人指出的那样,“不可能直接”。我猜你想要某种宋的索引键?只需在 Element 中创建另一个字段(成员变量)。将 Song 添加到集合时将其增加。

回答by EdenB

###################################################
###################################################
###################################################
AVOID THIS
###################################################
###################################################
###################################################

/*for (Song s: songList){
    System.out.println(s + "," + songList.indexOf(s); 
}*/

it is possible in linked list.

在链表中是可能的。

you have to make toString() in song class. if you don't it will print out reference of the song.

你必须在歌曲课上制作 toString() 。如果你不这样做,它会打印出这首歌的参考。

probably irrelevant for you by now. ^_^

现在可能与您无关。^_^

回答by Erik Brandsberg

Example from current code I'm working with:

我正在使用的当前代码示例:

int index=-1;
for (Policy rule : rules)
{   
     index++;
     // do stuff here
}

Lets you cleanly start with an index of zero, and increment as you process.

让您从零索引干净地开始,并在处理时递增。