I am using an Iterator to iterate through a collection and I want to get the current element's index.
我正在使用迭代器迭代一个集合,我想得到当前元素的索引。
How can I do that?
我怎样才能做到这一点?
11 个解决方案
#1
75
Use your own variable and increment it in the loop.
使用您自己的变量并在循环中递增它。
#2
80
I had the same question and found using a ListIterator worked. Similar to the test above:
我有同样的问题,发现使用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());
}
Make sure you call the nextIndex BEFORE you actually get the next().
确保在实际获得next()之前调用nextIndex。
#3
21
Here's a way to do it using your own variable and keeping it concise:
这是使用您自己的变量并保持简洁的方法:
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);
}
Output (you guessed it):
输出(你猜对了):
0: zero
1: one
2: two
The advantage is that you don't increment your index within the loop (although you need to be careful to only call Iterator#next once per loop - just do it at the top).
优点是你不要在循环中增加索引(尽管你需要注意每个循环只调用Iterator#next - 只需在顶部执行)。
#4
19
You can use ListIterator
to do the counting:
您可以使用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);
}
#5
9
What kind of collection? If it's an implementation of the List interface then you could just use it.nextIndex() - 1
.
什么样的收藏?如果它是List接口的实现,那么你可以使用it.nextIndex() - 1。
#6
4
Use a ListIterator to iterate through the Collection. If the Collection is not a List to start with use Arrays.asList(Collection.toArray())
to turn it into a List first.
使用ListIterator迭代Collection。如果Collection不是开始的List,请使用Arrays.asList(Collection.toArray())将其首先变为List。
#7
2
Use an int and increment it within your loop.
使用int并在循环中递增它。
#8
0
All you need to use it the iterator.nextIndex() to return the current index that the iterator is on. This could be a bit easier than using your own counter variable (which still works also).
您只需要使用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);
}
}
This code will go through the list1 list one item at a time and print the item's text, then "is at index" then it will print the index that the iterator found it at. :)
这段代码将一次通过list1列表一个项目并打印项目的文本,然后“is is index”然后它将打印迭代器找到它的索引。 :)
#9
0
See here.
看这里。
iterator.nextIndex()
would provide index of element that would be returned by subsequent call to next()
.
iterator.nextIndex()将提供后续调用next()返回的元素索引。
#10
0
Though you already had the answer, thought to add some info.
虽然你已经有了答案,但想添加一些信息。
As you mentioned Collections explicitly, you can't use listIterator
to get the index for all types of collections.
正如您明确提到的集合,您不能使用listIterator来获取所有类型的集合的索引。
List interfaces - ArrayList, LinkedList, Vector and Stack.
列表接口 - ArrayList,LinkedList,Vector和Stack。
Has both iterator()
and listIterator()
有iterator()和listIterator()
Set interfaces - HashSet, LinkedHashSet, TreeSet and EnumSet.
设置接口 - HashSet,LinkedHashSet,TreeSet和EnumSet。
Has only iterator()
只有迭代器()
Map interfaces - HashMap, LinkedHashMap, TreeMap and IdentityHashMap
映射接口 - HashMap,LinkedHashMap,TreeMap和IdentityHashMap
Has no iterators, but can be iterated using through the keySet()
/ values()
or entrySet()
as keySet()
and entrySet()
returns Set
and values()
returns Collection
.
没有迭代器,但可以通过keySet()/ values()或entrySet()使用keySet()进行迭代,并且entrySet()返回Set,values()返回Collection。
So its better to use iterators()
with continuous increment of a value to get the current index for any collection type.
因此,最好使用迭代器()连续递增值来获取任何集合类型的当前索引。
#11
-3
just do something like this:
只是做这样的事情:
for (AbstractDevice device : mDevicesList){
int curIndex = mDevicesList.indexOf(device));
}
#1
75
Use your own variable and increment it in the loop.
使用您自己的变量并在循环中递增它。
#2
80
I had the same question and found using a ListIterator worked. Similar to the test above:
我有同样的问题,发现使用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());
}
Make sure you call the nextIndex BEFORE you actually get the next().
确保在实际获得next()之前调用nextIndex。
#3
21
Here's a way to do it using your own variable and keeping it concise:
这是使用您自己的变量并保持简洁的方法:
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);
}
Output (you guessed it):
输出(你猜对了):
0: zero
1: one
2: two
The advantage is that you don't increment your index within the loop (although you need to be careful to only call Iterator#next once per loop - just do it at the top).
优点是你不要在循环中增加索引(尽管你需要注意每个循环只调用Iterator#next - 只需在顶部执行)。
#4
19
You can use ListIterator
to do the counting:
您可以使用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);
}
#5
9
What kind of collection? If it's an implementation of the List interface then you could just use it.nextIndex() - 1
.
什么样的收藏?如果它是List接口的实现,那么你可以使用it.nextIndex() - 1。
#6
4
Use a ListIterator to iterate through the Collection. If the Collection is not a List to start with use Arrays.asList(Collection.toArray())
to turn it into a List first.
使用ListIterator迭代Collection。如果Collection不是开始的List,请使用Arrays.asList(Collection.toArray())将其首先变为List。
#7
2
Use an int and increment it within your loop.
使用int并在循环中递增它。
#8
0
All you need to use it the iterator.nextIndex() to return the current index that the iterator is on. This could be a bit easier than using your own counter variable (which still works also).
您只需要使用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);
}
}
This code will go through the list1 list one item at a time and print the item's text, then "is at index" then it will print the index that the iterator found it at. :)
这段代码将一次通过list1列表一个项目并打印项目的文本,然后“is is index”然后它将打印迭代器找到它的索引。 :)
#9
0
See here.
看这里。
iterator.nextIndex()
would provide index of element that would be returned by subsequent call to next()
.
iterator.nextIndex()将提供后续调用next()返回的元素索引。
#10
0
Though you already had the answer, thought to add some info.
虽然你已经有了答案,但想添加一些信息。
As you mentioned Collections explicitly, you can't use listIterator
to get the index for all types of collections.
正如您明确提到的集合,您不能使用listIterator来获取所有类型的集合的索引。
List interfaces - ArrayList, LinkedList, Vector and Stack.
列表接口 - ArrayList,LinkedList,Vector和Stack。
Has both iterator()
and listIterator()
有iterator()和listIterator()
Set interfaces - HashSet, LinkedHashSet, TreeSet and EnumSet.
设置接口 - HashSet,LinkedHashSet,TreeSet和EnumSet。
Has only iterator()
只有迭代器()
Map interfaces - HashMap, LinkedHashMap, TreeMap and IdentityHashMap
映射接口 - HashMap,LinkedHashMap,TreeMap和IdentityHashMap
Has no iterators, but can be iterated using through the keySet()
/ values()
or entrySet()
as keySet()
and entrySet()
returns Set
and values()
returns Collection
.
没有迭代器,但可以通过keySet()/ values()或entrySet()使用keySet()进行迭代,并且entrySet()返回Set,values()返回Collection。
So its better to use iterators()
with continuous increment of a value to get the current index for any collection type.
因此,最好使用迭代器()连续递增值来获取任何集合类型的当前索引。
#11
-3
just do something like this:
只是做这样的事情:
for (AbstractDevice device : mDevicesList){
int curIndex = mDevicesList.indexOf(device));
}