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

前端 未结 19 1400
南方客
南方客 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:14

    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

提交回复
热议问题