Java ArrayList indexOf() 返回错误的索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3113696/
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
ArrayList indexOf() returns wrong index?
提问by Morten Priess
I have a problem with ArrayList. I'm using ArrayList like this:
我对 ArrayList 有问题。我正在像这样使用 ArrayList:
private ArrayList<Playlist> mPlaylists;
where Playlist is a class inherited from another ArrayList. I do the following:
其中 Playlist 是从另一个 ArrayList 继承的类。我执行以下操作:
p = new Playlist(...some parameters...);
mPlaylists.add(p);
Later, when I use 'p' to get the index in the list:
后来,当我使用 'p' 获取列表中的索引时:
int index = mPlaylists.indexOf(p);
an index of '1' is returned, even though inspection of the list clearly shows that it's index '4'.
返回索引 '1',即使检查列表清楚地显示它是索引 '4'。
Does anybody know why this fails? Thanks.
有谁知道为什么这会失败?谢谢。
B.R. Morten
BR莫腾
Edit:Same problem without indexOf(), using equals():
编辑:没有 indexOf() 的相同问题,使用 equals():
private int GetIndex(Playlist playlist) {
for (int i = 0; i < mPlaylists.size(); i++) {
if (mPlaylists.get(i).equals(playlist)) {
return i;
}
}
return -1;
}
New edit:This WORKS!:
新编辑:这有效!:
private int getIndex(Playlist playlist) {
for (int i = 0; i < mPlaylists.size(); i++) {
if (mPlaylists.get(i) == playlist) {
return i;
}
}
return -1;
}
Solution:As suggested, I changed the Playlist class to not enherit from ArrayList, but rather keeping an instance privately. It turned out that I only had to implement 4 ArrayList methods.
解决方案:按照建议,我将 Playlist 类更改为不从 ArrayList 继承,而是私下保留一个实例。结果证明我只需要实现 4 个 ArrayList 方法。
This does the trick; Now indexOf() returns the correct object!
这就是诀窍;现在 indexOf() 返回正确的对象!
Thanks to all contributors!
感谢所有贡献者!
采纳答案by OscarRyz
Most likely your PlayList
messed up with the default ArrayList equals
implementation, because the way indexOf
is calculated to something like:
最有可能你PlayList
搞砸使用默认的ArrayListequals
实现,因为该方法indexOf
是计算到类似:
indexOf(Object o)
if( o == null ) then iterate until null is found and return that index
if( o != null ) iterate until o.equals( array[i] ) is found and return taht index
else return -1
end
So, you are doing something funny with your .equals method or your are accidentally inserting another element in the list when you think it is at the end.
因此,您对 .equals 方法做了一些有趣的事情,或者当您认为它在末尾时不小心在列表中插入了另一个元素。
EDIT
编辑
As per your edit... see? Your .equals()
method is broken.
根据您的编辑...看到了吗?你的.equals()
方法坏了。
Consider doing a good review and make sure it adheres to the description defined in Object.equals
考虑进行良好的并确保它符合Object.equals 中定义的描述
回答by Russ Clark
I'm not sure why you are having this problem, but I think if I were you I would choose to use the newer Generic List to create your list like this:
我不确定您为什么会遇到此问题,但我想如果我是您,我会选择使用较新的通用列表来创建您的列表,如下所示:
List<Playlist> mPlaylists = new List<Playlist>();
p = new Playlist(<some parameters>);
mPlaylists.Add(p);
回答by Dave
From the API:
从API:
int indexOf(Object o)
:Returns the index of the first occurrence of the specified element in this list, or
-1
if this list does not contain the element. More formally, returns the lowest index i such that(o==null ? get(i)==null : o.equals(get(i)))
, or-1
if there is no such index.
int indexOf(Object o)
:返回此列表中指定元素第一次出现的索引,或者
-1
如果此列表不包含该元素。更正式地,返回最低索引 i(o==null ? get(i)==null : o.equals(get(i)))
,-1
如果,或者如果没有这样的索引。
So the answer is that you need to override .equals()
in Playlist
.
所以答案是你需要.equals()
在Playlist
.
回答by Eyal Schneider
There may be many reasons for this behavior:
这种行为可能有很多原因:
1) If multiple elements in an ArrayList are equal (according to equals method), then the firstone is returned. Maybe you simply have multiple identical objects.
1) 如果 ArrayList 中的多个元素相等(根据 equals 方法),则返回第一个。也许你只是有多个相同的对象。
2) Your PlayList class extends ArrayList (I am not sure it's a good idea). Therefore, if you didn't override the equals method, the comparison is based on the sequence of elements only. For example, any two empty PlayList instances will be considered equal.
2)您的 PlayList 类扩展了 ArrayList (我不确定这是个好主意)。因此,如果您没有覆盖 equals 方法,则比较仅基于元素的序列。例如,任何两个空的播放列表实例都将被视为相等。
3) If you DID override equals, check your implementation. It must return true for a comparison with the same reference, and in your case it doesn't.
3) 如果您 DID 覆盖等于,请检查您的实现。对于与相同引用的比较,它必须返回 true,而在您的情况下则不然。