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

时间:2021-07-14 17:28:16

This question already has an answer here:

这个问题已经有了答案:

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

7 个解决方案

#1


251  

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"

原因是您可以使用压缩的语法来循环遍历任何可迭代,并且不能保证值实际上有一个“索引”

#2


24  

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. ^_^

现在可能已经无关紧要了。^ _ ^

#3


12  

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循环。

#4


12  

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

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

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

#5


6  

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)

#6


1  

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.

正如其他人指出的,“不可能直接”。我猜你是想要一些索引键?只需在元素中创建另一个字段(成员变量)。将歌曲添加到集合时增加它。

#7


-3  

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.

让您可以从0的索引开始,并在处理过程中递增。

#1


251  

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"

原因是您可以使用压缩的语法来循环遍历任何可迭代,并且不能保证值实际上有一个“索引”

#2


24  

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. ^_^

现在可能已经无关紧要了。^ _ ^

#3


12  

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循环。

#4


12  

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

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

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

#5


6  

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)

#6


1  

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.

正如其他人指出的,“不可能直接”。我猜你是想要一些索引键?只需在元素中创建另一个字段(成员变量)。将歌曲添加到集合时增加它。

#7


-3  

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.

让您可以从0的索引开始,并在处理过程中递增。