0

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;
        }

}
0

3 Answers 3

1

You could use AbstractMap.SimpleEntry:

public static SimpleEntry<Integer, Integer> getMaxValue(int[] values){  
    int maxValue = Integer.MIN_VALUE;
    int maxIndex = -1;
    for(int i=0;i<values.length;i++){  
        if(values[i] >= maxValue){  
            maxValue = values[i];
            maxIndex = i;
        }  
    }
    return new SimpleEntry<Integer, Integer>( maxIndex, maxValue);
}

public static void main(String[] args) {
    SimpleEntry<Integer, Integer> maxEntry = getMaxValue( new int[]{1,2,3});

    System.out.println( "value["+ maxEntry.getKey() + "] = " + maxEntry.getValue() + " is the largest element");
    //value[2] = 3 is the largest element

}
Sign up to request clarification or add additional context in comments.

3 Comments

Don't overcompicate things... its a simple task
@firephil there is no way in java to return multiple values without using an object, might as well use an existing class than to create a new one.
returning the maxIndex is the simplest and best solution no need to complicate things
1

Hope the below code may help you

public static int getMaxIndex(int[] values) {
    int maxValue = values[0];
    int maxIndex = 0;
    for (int i = 0; i < values.length; i++) {
        if (values[i] > maxValue) {
            maxValue = values[i];
            maxIndex = i;
        }
    }
    return maxIndex;
}

1 Comment

And I would prefer to start with Integer.MIN_VALUE in maxValue and -1 in maxIndex.
1

You create a instance variable called position:

public class ArrayExample 
    {
    private static int position = 0;
     ...

    }

in your getMaxValue you also save the position along with the maxValue:

    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]; 
                position = i;
        }  
        }  
    return maxValue;  
} 

then you print it outside:

System.out.println("Maximum Value = " + getMaxIndex(values) + "in position" + position);  

Or another approach is to, return the position instead of the maxValue:

public static int getMaxValue(int[] values){  
            int maxValue = values[0];  
            int position = 0;
            for(int i=0;i<values.length;i++){  
                if(values[i] > maxValue){  
                    maxValue = values[i]; 
                    position = i;
            }  
            }  
        return position;  
    } 

and outside the you do:

int position = getMaxIndex(values);
System.out.println("Maximum Value = " + values[position] + "in position" + position);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.