for (Object object : list) [java] 和索引元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2858103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
for (Object object : list) [java] and index element
提问by EugeneP
Is there a way to get an element id of a list to get it later through list.get(index)
有没有办法获取列表的元素 id 以便稍后通过 list.get(index)
when using
使用时
for(Object obj: o)
construction
建造
Only define a local var and manually incrementing it? Anything simpler?
只定义一个局部变量并手动增加它?有没有更简单的?
采纳答案by polygenelubricants
No, for-each
loop doesn't keep track of index. You can either use a regular indexed loop, or do something like this:
不,for-each
循环不跟踪索引。您可以使用常规索引循环,也可以执行以下操作:
int idx = 0;
for (Object o : list) {
...
idx++;
}
This is risky since break
/continue
will make idx
goes out of sync, so do use this infrequently, and only when the body is simple and only a few lines long.
这是有风险的,因为break
/continue
会使 makeidx
不同步,所以不要经常使用它,并且只有当主体很简单并且只有几行长时才使用它。
If the elements are distinct, List.indexOf
would also work, albeit at O(N)
, and at that point you may want to consider a Set
(unordered, but guaranteed distinct).
如果元素是不同的,List.indexOf
也可以工作,尽管在O(N)
,此时您可能需要考虑 a Set
(无序,但保证不同)。
It should also be said that sometimes using a listIterator()
also alleviates the need of an explicit index while iterating.
还应该说,有时使用 alistIterator()
也减轻了迭代时显式索引的需要。
A ListIterator
supports add
, set
and remove
operations.
一个ListIterator
支持add
,set
和remove
操作。
This is another clear advantage List
has over arrays as far as iteration mechanism goes.
List
就迭代机制而言,这是另一个明显优于数组的优势。
回答by danben
This question is not worded very well, but from what I can tell you would like to find the position of a specific Object
within a List<Object>
, and then retrieve it later via that index?
这个问题是不是措辞非常好,但我可以告诉你想找到一个特定的位置Object
内List<Object>
,然后通过该指数后来找回?
First of all, if you know the Object
you are looking for then you should not need to have to find it in the List
, since you have it already.
首先,如果您知道Object
您正在寻找的 ,那么您不需要在 中找到它List
,因为您已经有了它。
That said, you could easily do something like this:
也就是说,您可以轻松地执行以下操作:
int index = -1;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(myObject)) {
index = i;
break;
}
}
But I would take a second look at your application to determine whether this is really something that is necessary for you to do.
但我会再次查看您的申请,以确定这是否真的是您必须做的事情。
回答by Stephen C
The simple answer is No.
简单回答是不。
The best I can think of is to combine iteration and indexing as follows:
我能想到的最好的方法是将迭代和索引结合起来,如下所示:
int idx = 0;
for (Iterator<Object> it = list.iterator(); it.hasNext(); idx++) {
Object o = it.next();
// ...
}
This approach has the advantage that break
and continue
won't mess up the idx
calculations.
这种方法的优点是break
和continue
不会弄乱的idx
计算。
On the other hand, if the list
is an ArrayList
and you want to keep track of the index, you are probably better just using a variation of
另一方面,如果list
是一个ArrayList
并且你想跟踪索引,你可能最好只使用一个变体
for (idx = 0; idx < list.size(); idx++) {
Object o = list.get(idx);
// ...
}
回答by Justin
You need to use:
您需要使用:
list.indexOf(o);
The documentation is here
文档在这里
@Eugene I see you mention in a comment to another answer that you plan to store the index of an object (with in the object in question?), that is probably not a good idea, be very careful since an objects index can change.
@Eugene 我看到你在另一个答案的评论中提到你计划存储一个对象的索引(在有问题的对象中?),这可能不是一个好主意,因为对象索引可能会改变,所以要非常小心。