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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:46:15  来源:igfitidea点击:

for (Object object : list) [java] and index element

javalistloops

提问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-eachloop 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/continuewill make idxgoes 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.indexOfwould 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 ListIteratorsupports add, setand removeoperations.

一个ListIterator支持addsetremove操作。

This is another clear advantage Listhas 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 Objectwithin a List<Object>, and then retrieve it later via that index?

这个问题是不是措辞非常好,但我可以告诉你想找到一个特定的位置ObjectList<Object>,然后通过该指数后来找回?

First of all, if you know the Objectyou 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 breakand continuewon't mess up the idxcalculations.

这种方法的优点是breakcontinue不会弄乱的idx计算。

On the other hand, if the listis an ArrayListand 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 我看到你在另一个答案的评论中提到你计划存储一个对象的索引(在有问题的对象中?),这可能不是一个好主意,因为对象索引可能会改变,所以要非常小心。