I have a question. I am trying to display the maximum value's index in an array. I was able to obtain the maximum value; however, I don't know to display the index for that maximum value.
For instance, if index [3] had a value of 5 and were the largest element in an array, it would print:
value[3] = 5 is the largest element
How do I do that?
import java.util.Scanner;
public class ArrayExample
{
public static void main(String[] args)
{
//Question 1
int values[] = new int[5] ;
System.out.println("Please enter 5 values for this array: ");
Scanner keyboard = new Scanner(System.in);
System.out.println("Maximum Value = " + getMaxIndex(values) + getMaxValue(values));
}
public static int getMaxValue(int[] values){
int maxValue = values[0];
for(int i=0;i<values.length;i++){
if(values[i] > maxValue){
maxValue = values[i];
}
}
return maxValue;
}
public static int getHighestMonth(int[] values) {
int highest = 0;
for (int i = 0; i < 5; i++) {
if (values[i] > values[highest]) {
highest = i;
}
}
return highest;
}
}