Java 使用迭代器时如何获取当前循环索引?
我正在使用迭代器对集合进行迭代 我想得到当前元素的索引Java 使用迭代器时如何获取当前循环索引?,java,iterator,Java,Iterator,我正在使用迭代器对集合进行迭代 我想得到当前元素的索引 我该怎么做呢?使用int并在循环中递增。使用您自己的变量并在循环中递增。什么类型的集合?如果它是列表接口的一个实现,那么您可以使用it.nextIndex()-1使用来迭代集合。如果集合不是一个列表,请首先使用Arrays.asList(Collection.toArray())将其转换为列表。请参阅 iterator.nextIndex()将提供后续调用next()返回的元素索引 以下是一种使用您自己的变量并保持简洁的方法: List&l
我该怎么做呢?使用int并在循环中递增。使用您自己的变量并在循环中递增。什么类型的集合?如果它是列表接口的一个实现,那么您可以使用
it.nextIndex()-1
使用来迭代集合。如果集合不是一个列表,请首先使用Arrays.asList(Collection.toArray())
将其转换为列表。请参阅
iterator.nextIndex()
将提供后续调用next()
返回的元素索引 以下是一种使用您自己的变量并保持简洁的方法:
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);
}
优点是在循环中不增加索引(尽管需要小心,每个循环只调用迭代器#next一次-只在顶部执行一次)。我有同样的问题,发现使用
列表迭代器
是可行的。与上述测试类似:
List<String> list = Arrays.asList("zero", "one", "two");
ListIterator<String> iter = list.listIterator();
while (iter.hasNext()) {
System.out.println("index: " + iter.nextIndex() + " value: " + iter.next());
}
List List=Arrays.asList(“零”、“一”、“二”);
ListIterator iter=list.ListIterator();
while(iter.hasNext()){
System.out.println(“索引:+iter.nextIndex()+”值:+iter.next());
}
确保在实际获取
next()
之前调用nextIndex()
,您可以使用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);
}
final List=Arrays.asList(“零”、“一”、“二”、“三”);
for(final ListIterator it=list.ListIterator();it.hasNext();){
最后一个字符串s=it.next();
System.out.println(it.previousIndex()+“:”+s);
}
只需使用iterator.nextIndex()即可返回迭代器所在的当前索引。这可能比使用自己的计数器变量(仍然有效)要容易一些
publicstaticvoidmain(字符串[]args){
字符串[]str1={“列表项1”、“列表项2”、“列表项3”、“列表项4”};
List list1=新的ArrayList(Arrays.asList(str1));
ListIterator it=list1.ListIterator();
int x=0;
//iterator.nextIndex()将为您返回索引。
while(it.hasNext()){
int i=it.nextIndex();
System.out.println(it.next()+“位于索引“+i”);
}
}
这段代码将一次遍历列表1中的一个项目并打印该项目的文本,然后“在索引中”,然后它将打印迭代器找到它的索引。:) 就这样做吧:
ListIterator<String> it = list1.listIterator();
int index = -1;
while (it.hasNext()) {
index++;
String value = it.next();
//At this point the index can be checked for the current element.
}
ListIterator it=list1.ListIterator();
int指数=-1;
while(it.hasNext()){
索引++;
字符串值=it.next();
//此时可以检查当前元素的索引。
}
虽然你已经得到了答案,但还是想添加一些信息
正如您明确提到的集合,您不能使用listIterator
获取所有类型集合的索引
列出接口-ArrayList、LinkedList、Vector和Stack
同时具有<代码>迭代器()和<代码>列表迭代器()
设置接口-哈希集、LinkedHashSet、树集和枚举集
只有迭代器()
映射接口-HashMap、LinkedHashMap、TreeMap和IdentityHashMap
没有迭代器,但可以使用keySet()
/values()
或entrySet()
作为keySet()
和entrySet()
返回Set
和values()
返回Collection
因此,最好使用具有连续增量值的
迭代器()
来获取任何集合类型的当前索引。这将是最简单的解决方案
std::vector<double> v (5);
for(auto itr = v.begin();itr != v.end();++itr){
auto current_loop_index = itr - v.begin();
std::cout << current_loop_index << std::endl;
}
标准:向量v(5);
对于(自动itr=v.begin();itr!=v.end();++itr){
自动电流环路索引=itr-v.begin();
std::cout可能重复@finnw我不认为它们是重复的。这个问题是使用迭代器询问的,另一个是用于每个循环。这两个问题都通过类似的方法解决,因此答案是重复的,而不是问题。如果您自己创建迭代器,您也可以使用ListIterator,而不需要单独的int变量。如果你对Arrays.asList使用“静态导入”,那么你可以只写
asList(“零”、“一”、“二”)
,这正是我在阅读Paul的答案之前所做的。我强烈反对你的方式,因为我看不到它有任何优势。你认为有优势吗(除了上面提到的那一个)。为什么不为每个循环使用一个?如果您使用自己的变量,则无需明确定义迭代器。@progressive_重载仅当您需要迭代器时(根据问题,例如传递到库),示例中没有显示。在本例中,循环外有一个变量,需要小心调用#next一次。在Paul的示例中,循环外没有变量,但需要小心调用#next和#nextIndex一次(在实践中,如果多次使用,它们将被拉入局部变量中,该示例没有显示)。实际上,您的代码被关闭了一次,因为它试图在调用.next()后显示索引。@HenrikAastedSørensen我的代码没有被关闭一次,但我可以理解为什么它看起来是这样的。第一次,它被关闭了。next()它将返回索引0处的列表项。继续运行代码,您将看到,它工作得很好:)调用indexOf()将需要额外扫描设备列表。只需增加一个本地计数器会更快。同意。这不是最有效的解决方案。看起来您更新了示例以提高效率。但也请参阅@mateusz dymczyk关于It.nextIndex()
的建议。当集合是列表时非常有用。感谢您的提醒
ListIterator<String> it = list1.listIterator();
int index = -1;
while (it.hasNext()) {
index++;
String value = it.next();
//At this point the index can be checked for the current element.
}
std::vector<double> v (5);
for(auto itr = v.begin();itr != v.end();++itr){
auto current_loop_index = itr - v.begin();
std::cout << current_loop_index << std::endl;
}