How to find array index of largest value?

后端 未结 8 1263
小蘑菇
小蘑菇 2020-12-11 02:33

The title above sums up my question, to clarify things an example is:

array[0] = 1
array[1] = 3
array[2] = 7  // largest
array[3] = 5

so th

8条回答
  •  半阙折子戏
    2020-12-11 02:35

    Another functional implementation

    int array[] = new int[]{1,3,7,5};        
    
    int maxIndex =IntStream.range(0,array.length)
                  .boxed()
                  .max(Comparator.comparingInt(i -> array[i]))
                  .map(max->array[max])
                  .orElse(-1);
    

提交回复
热议问题