Java如何在"for each"循环中获取当前索引或键呢?
下文笔者讲述for each循环输出索引或键的方法分享,如下所示
使用一个临时变量 for循环每运行一次 然后自增加一例:循环获取索引或键的示例
int index = 0; for(Element song : arrs) { System.out.println("Current index is: " + (index++)); } 或使用普通for循环 for(int i = 0; i < arrs.length; i++) { System.out.println("Current index is: " + i); }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。