Returns the index of the largest value in the array. If array is empty, -1 is returned. @param arr array @return index of largest value
(double[] arr)
| 132 | * @return index of largest value |
| 133 | */ |
| 134 | public static int argMax(double[] arr) { |
| 135 | if (arr.length == 0) return -1; |
| 136 | if (arr.length == 1) return 0; |
| 137 | if (arr.length == 2) return arr[0] >= arr[1] ? 0 : 1; |
| 138 | int index = 0; |
| 139 | for (int i = 1; i < arr.length; i++) { |
| 140 | if (arr[i] > arr[index]) index = i; |
| 141 | } |
| 142 | return index; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns the index of the smallest value in the array. If array is empty, -1 is returned. |