I've the below Java code.
import java.util.Arrays;
public class Cook {
public static void main(String[] args) {
int num[] = { 3, 1, 5, 2, 4 };
getMaxValue(num);
}
public static void getMaxValue(int[] num) {
int maxValue = num[0];
int getMaxIndex = 0;
for (int i = 1; i < num.length; i++) {
if (num[i] > maxValue) {
maxValue = num[i];
}
}
getMaxIndex = Arrays.asList(num).indexOf(maxValue);
System.out.println(getMaxIndex + " and " +maxValue);
}
}
In the above code I'm trying to retrieve the maximum value in the array and also its index, but here the output that I'm getting is
-1 and 5
The max value is returned fine, but not sure of what's wrong with the index. This should actually print 2, but it is printing -1, please let me know where am i going wrong and how can I fix this.
Thankd
