5

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

2
  • 2
    Do you really need Arrays...indexOf in this code snippet? Commented Dec 8, 2015 at 10:19
  • If you took the time to do a little debugging, and inspected what every single one of the whole of 2 (!) functions that you use does, you would know. Commented Dec 8, 2015 at 19:16

4 Answers 4

21

You should update the max index in the loop :

    int maxValue = num[0];
    int getMaxIndex = 0;
    for (int i = 1; i < num.length; i++) {
        if (num[i] > maxValue) {
            maxValue = num[i];
            getMaxIndex = i;
        }
    }

The reason Arrays.asList(num).indexOf(maxValue); returns -1 is that an array of primitives is converted by Arrays.asList to a List of a single element (the array itself), and that List doesn't contain maxValue (it only contains the original array).

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

Comments

6

Need to update index while iterating, getMaxIndex = i;

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 = i;
            }
        }
        System.out.println(getMaxIndex + " and " + maxValue);
    }

output

2 and 5

Below is something @Eran is referring to.

It is converted to List of size 1, containing a single element (the array itself).

As per Javadoc,indexOf

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

So it searches for maxValue inside List and not inside array stored in 0th index of List.

enter image description here

Comments

4

Everyone gives good hints, but no one explains in detail why it doesn't work.

Arrays.asList() is defined with the signature public static <T> List<T> asList(T... a) which takes a variable number of objects or just an array of objects.

However, int is a primitive type and not an object type. So Arrays.asList(num) is not interpreted as "take this array", but as "take this object as one object". The result is thus a List<int[]>, where the given number (of course) can't be found.

So it is better to keep the index while searching for the maximum, as the other answers already suggest.

Comments

1

above answers are correct but you can also do

import java.util.Arrays;

public class Cook {

    public static void main(String[] args) {
        Integer num[] = { 3, 1, 5, 2, 4 };
        getMaxValue(num);
    }

    public static void getMaxValue(Integer[] num) {
        int maxValue = Arrays.asList(num).get(0);
        int getMaxIndex = 0;
        for (int i = 1; i < num.length; i++) {
            if (Arrays.asList(num).get(i) > maxValue) {
                maxValue = Arrays.asList(num).get(i);
            }
        }
        getMaxIndex = Arrays.asList(num).indexOf(maxValue);
        System.out.println(getMaxIndex + " and " +maxValue);
    }
}

1 Comment

Why all these transformations with Arrays.asList ? If you're using an Integer[], just do: List<Integer> l = Arrays.asList(num); int max = Collections.max(l); int indexMax = l.indexOf(max);. 3 lines...

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.