如何从给定值找到的Java int数组的索引? [英] How to find index of int array in Java from a given value?
问题描述
我希望找到对一个int类型的Java数组为一定值的指数。
我怎样才能做到这一点?我可以应用的binarySearch方法非排序的数组?
下面是我尝试过,但有些时候它给出正确的答案,但大部分时间不正确。我究竟做错了什么?
Arrays.binarySearch(数组值)
;
整数[]数组= {1,2,3,4,5,6};Arrays.asList(阵列).indexOf(4);
请注意,此解决方案是线程安全的,因为它创建一个List类型的新对象。
你也不想在一个循环或类似的东西来调用这个,因为你每次都创建一个新对象
I am looking to find an index against a certain value in a Java array of type int.
How can I do this? Can I apply the binarySearch method to a non sorted array?
Below is what I tried, but some times it gives correct answer but most of the time incorrect. What am I doing wrong?
Arrays.binarySearch(array, value)
;
Integer[] array = {1,2,3,4,5,6};
Arrays.asList(array).indexOf(4);
Note that this solution is threadsafe because it creates a new object of type List.
Also you don't want to invoke this in a loop or something like that since you would be creating a new object every time
这篇关于如何从给定值找到的Java int数组的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!