在Java中,如何获取当前元素的索引?
for (Element song: question){
song.currentIndex(); //<<want the current index
}
在 PHP 中,你可以这样做:
foreach ($arr as $index => $value) {
echo "Key: $index; Value: $value";
}
你不能,你要么需要单独保留索引:
int index = 0;
for(Element song : question) {
System.out.println("Current index is: " + (index++));
}
或使用普通的 for 循环:
for(int i = 0; i < question.length; i++) {
System.out.println("Current index is: " + i);
}
原因是您可以使用压缩的 for 语法来循环任何Iterable,并且不能保证这些值实际上有一个“索引”
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)