-4

ArrayList.Indexof("Value")在我的一个应用程序中使用。ArrayList包含 400 多个值,我正在使用此列表在微调器中显示。我想将微调器移动到特定位置,因为我需要Indexof那个项目。我正在使用 Spinner setSelection,但这里ArrayList.Indexof("Value")返回 -1... 为什么???请帮我..

4

3 回答 3

6

请参阅javadoc

如果没有这样的索引,则返回 -1

于 2012-06-19T09:26:37.127 回答
1

它返回 -1 的事实告诉您 ArrayList 中不存在索引“Value”。为了比较,equals()使用。可能失败的两点:

  • 拼写,包括大小写 ("value".equals("Value") -> false)
  • 您的 ArrayList 不包含字符串,而是其他可能使用不同实现的东西String#equals(String)

如果您希望我们能够为您提供更多信息,我们需要一些代码...

于 2012-06-19T09:32:09.757 回答
0

I am using Spinner's setSelection but here ArrayList.Indexof("Value") returns -1

This tells you that the string is in fact not in the list. Period.

So what could really be going on?

@brimborium has identified a couple of possibilities:

  • the value in the list and the value that you are testing differ in case
  • the value in the list or the value that you are testing is not a String

And some others are:

  • one or other of the values has leading or trailing whitespace that doesn't show up
  • certain Unicode codepoints (characters) look the same when rendered but are different, and compare as different

Or ... your code either didn't add the value, or it removed it.

于 2012-06-19T10:50:09.923 回答