-3
A
回答
1
如果您的列表正在存储数字,您会收到-1,因为您将一个字符串传递给indexOf方法。
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.indexOf("1");//returns -1
list.indexOf(1);//returns 0
在indexOf
内部进行内部比较时,它会检查考虑类型的相等性。
1
“2”是一个字符串,而您的列表包含整数。尝试List.indexOf(new Integer(2));
0
使用List.indexOf(2);
代替List.indexOf("2");
因为,你的问题说,你有你的List
对象数值数据尝试,你将不得不通过一个参数为数字,而不是String
。除非您以String
的形式存储您的数据,否则您将收到-1
。
1
-1表示搜索值未包含在您的列表中。
是真正的字符串列表的内容?
0
您正在搜索2作为字符串。尝试List.indexOf(2)
0
正如您将2传递为“2”。它认为是一个字符串。 如果你想从列表中获取数据,你需要像List.indexOf(new Integer(2))那样传递; 或List.indexOf(new Integer(Any number));
0
正如Katja所说,您可能正在寻找您的清单中的错误类型(字符串)(我认为它是一个列表整数)。
参见下例:
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
System.out.println(list.indexOf("1"));
System.out.println(list.indexOf(1));
输出:
-1
0
相关问题
- 1. List.IndexOf返回1 c#
- 2. indexOf()函数返回-1值
- 3. 值返回1.#INF000
- 4. 获取行数只返回值1
- 5. 回调函数返回返回($ var&1)?
- 6. 返回行数= 1
- 7. C#Indexof返回值-1
- 8. @ Html.DropDownList只返回1值
- 9. xquery返回值为1
- 10. getyx返回-1 -1
- 11. 返回-1和1之间的值的散列函数
- 12. 返回除-1指数
- 13. json_decode返回数组1
- 14. Unaccent函数返回1
- 15. Dll函数返回1
- 16. Socket_read返回'1'..?
- 17. getch返回-1?
- 18. request.contentLength返回-1
- 19. db2_num_rows()返回-1
- 20. GetMenuItemCount返回-1
- 21. PipedInputStream.read()返回-1
- 22. ExecuteNonQuery返回-1
- 23. awk:mktime返回-1
- 24. Indexof()返回-1
- 25. getLastVisiblePosition返回-1
- 26. Popen.poll()返回1
- 27. Javascript函数只返回1个值而不是数组数组
- 28. Random.nextInt(1);只返回1
- 29. 如何返回值1存储过程
- 30. SELECT COUNT(重复值)返回1