Returns the index of the first array element equal to the key, or -1 if no such element. This function can be also used as 'contains' by checking for the return value being >= 0.
(int[] array, int key)
| 151 | * the return value being >= 0. |
| 152 | */ |
| 153 | public static int indexOf(int[] array, int key) { |
| 154 | for (int i=0; i<array.length; i++) |
| 155 | if (array[i] == key) |
| 156 | return i; |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | /** Returns the index of the first array element equal to the key, |
| 161 | * or -1 if no such element. If the key is null, returns the index |