Everyone gives good clues, but no one explains in detail why this does not work.
Arrays.asList() defined by the signature public static <T> List<T> asList(T... a) , which accepts a variable number of objects or just an array of objects.
However, int is a primitive type, not an object type. Thus, Arrays.asList(num) not interpreted as โtake this arrayโ, but as โtake this object as one objectโ. Thus, the result will be List<int[]> , where the given number (of course) cannot be found.
Therefore, it is better to keep the index when looking for the maximum, as other answers already indicate.
source share