How to find the index of an element in an int array?

前端 未结 19 1405
南方客
南方客 2020-11-27 02:56

How can I find an index of a certain value in a Java array of type int?

I tried using Arrays.binarySearch on my unsorted array, it only som

19条回答
  •  一个人的身影
    2020-11-27 03:11

    You can use modern Java to solve this problem. Please use the code below:

    static int findIndexOf(int V, int[] arr) {
            return IntStream.range(1, arr.length).filter(i->arr[i]==V).findFirst().getAsInt();
        }
    

提交回复
热议问题