回答
使用自己的變量,在環加一。
但是請參閱@ mateusz-dymczyk有關'it.nextIndex()'的建議。收集是List時有用。 – noamtm 2016-09-06 08:57:49
使用int和你的循環中增加它。
什麼樣的收集?如果它是List接口的實現,那麼你可以使用it.nextIndex() - 1
。
使用ListIterator通過收集迭代。如果集合不是以使用Arrays.asList(Collection.toArray())
首先將其變成列表開始的列表開始。
見here。
iterator.nextIndex()
將提供將由後續調用返回到next()
元素的索引。
接口迭代器沒有nextIndex()方法。你需要爲此明確使用一個ListIterator,但是OP對Iterator有特別的質疑。 – 2018-02-21 11:10:26
這裏是一種使用自己的變量,並保持其簡潔的做到這一點:
List<String> list = Arrays.asList("zero", "one", "two");
int i = 0;
for (Iterator<String> it = list.iterator(); it.hasNext(); i++) {
String s = it.next();
System.out.println(i + ": " + s);
}
輸出(你猜對了):
0: zero
1: one
2: two
的好處是,你不增加你的循環中的索引(儘管你需要小心,每次循環只接下來調用Iterator# - 只需在頂部執行)。
如果你自己創建迭代器,你也可以使用ListIterator,並且不需要單獨的int變量。 – 2012-08-20 07:14:14
如果你爲Arrays.asList使用'靜態導入',那麼你可以直接寫'asList(「zero」,「one」,「two」)' – karmakaze 2015-06-23 15:48:30
,這正是我在閱讀Paul的答案之前所做的。我強烈勸阻你,因爲我沒有看到任何好處。你認爲有一個優點(除了提到的那個)。你爲什麼不使用for-each循環?如果使用自己的變量,則明確定義迭代器不是必需的。 – 2016-05-20 08:08:35
我有同樣的問題,使用的ListIterator工作中。類似於上面的測試:
List<String> list = Arrays.asList("zero", "one", "two");
ListIterator iter = list.listIterator();
while (iter.hasNext()) {
System.out.println("index: " + iter.nextIndex() + " value: " + iter.next());
}
確保您調用nextIndex之前,你實際上得到下一個()。
感謝您提及'確保您調用nextIndex之前,您實際上得到下一個()' – 2016-12-22 11:27:51
您可以使用ListIterator
做計數:
final List<String> list = Arrays.asList("zero", "one", "two", "three");
for (final ListIterator<String> it = list.listIterator(); it.hasNext();) {
final String s = it.next();
System.out.println(it.previousIndex() + ": " + s);
}
所有你需要使用它的iterator.nextIndex()迭代器上,返回當前索引。這可能比使用自己的計數器變量更容易一些(它仍然有效)。
public static void main(String[] args) {
String[] str1 = {"list item 1", "list item 2", "list item 3", "list item 4"};
List<String> list1 = new ArrayList<String>(Arrays.asList(str1));
ListIterator<String> it = list1.listIterator();
int x = 0;
//The iterator.nextIndex() will return the index for you.
while(it.hasNext()){
int i = it.nextIndex();
System.out.println(it.next() + " is at index" + i);
}
}
此代碼將通過列表1列表一個項目的時間和打印項目的文本,然後在「在索引」,那麼它將打印索引迭代器發現它的。 :)
只是做這樣的事情:
for (AbstractDevice device : mDevicesList){
int curIndex = mDevicesList.indexOf(device));
}
的
調用indexOf()將需要額外掃描設備列表。簡單地增加一個本地計數器會更快。 – 2014-03-13 13:24:34
- 1. SSRS - 獲取當前tablix迭代索引
- 2. 循環索引在迭代
- 3. 如何在NSEnumerator迭代中獲取當前對象的索引?
- 4. 如何在foreach循環中獲取當前數組索引?
- 5. Ansible - 迭代主機時獲取當前循環計數
- 6. 使用$ .each循環遍歷結果時,如何引用當前迭代的索引?
- 7. 在XPath嵌套迭代中獲取當前索引
- 8. 如何獲取FOREACH迭代的索引
- 9. 從(int&i:i)獲取當前迭代器
- 10. 如何在批量迭代數組的同時引用當前索引?
- 11. 獲取Powershell循環中當前項目的索引?
- 12. 在while循環中使用迭代器
- 13. Java - 獲取循環迭代器到循環內的函數?
- 14. 循環播放時獲取索引
- 15. 如何在C++中使用迭代器時獲得循環次數?
- 16. 如何在迭代器中引用當前對象
- 17. 在for循環之前定義循環迭代器?
- 18. 使用迭代器獲取向量的索引
- 19. 如何獲取ByteString中迭代器的當前位置?
- 20. 如何使用範圍循環迭代
- 21. NoSuchElementException當使用Scanner對象並在循環中迭代時
- 22. 從循環中間使用迭代器
- 23. 迭代使用-的循環
- 24. 如何使用Jquery獲取我的表的當前行索引?
- 25. 如何使用jCarousel Lite獲取當前索引?
- 26. :如何控制循環迭代器?
- 27. 從前一個循環獲取當前循環中使用的值
- 28. Java,我如何獲得當前的索引/鍵在「for each」循環中
- 29. 在TBB中獲取迭代器parallel_for循環
- 30. 用盡迭代器,但在For循環
可能重複[是否有訪問Java的for-each循環迭代櫃檯的方式?(http://stackoverflow.com/questions/477550/is-there-a-way-to-access-an-iteration-counter-in-javas-for-each-loop) – finnw 2010-07-25 16:10:34
@finnw我不認爲它們是重複的。這個問題是使用Iterator,另一個是使用for-each循環。這兩個問題都是通過類似的方法解決的,所以答案是重複的而不是問題。 – Robert 2014-06-03 11:03:31