为什么indexOf找不到对象? [英] Why is indexOf failing to find the object?
本文介绍了为什么indexOf找不到对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个整数列表,并试图返回特定值的索引. 数组是3,8,2,5,1,4,7,6,我想返回indexOf(3),应该为0.
I created an integer list and am trying to return the index of a specific value. The array is 3,8,2,5,1,4,7,6 and I want to return the indexOf(3), which should be 0.
导入java.util.*后,我在Eclipse Java Scrapbook中尝试了以下操作:
I've tried the following in the Eclipse Java Scrapbook after importing java.util.*:
int[] A = {3,8,2,5,1,4,7,9};
Arrays.asList(A).indexOf(3)
我也尝试过:
int[] A = {3,8,2,5,1,4,7,6};
ArrayList<Integer> l = new ArrayList(Arrays.asList(A));
l.indexOf(3)
两个都返回-1.为什么?如何使它按预期工作?
Both are returning -1. Why? How to get this to work as expected?
推荐答案
为使其正常运行,应该为Integer[]
而不是int[]
.
It should be Integer[]
not int[]
in order to make it work.
Integer[] A = {3,8,2,5,1,4,7,9};
final int i = Arrays.asList(A).indexOf(3);
System.out.println("i = " + i); // prints '0'
这篇关于为什么indexOf找不到对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文