Invalid array index index value

I have Java code below.

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 code above, I am trying to get the maximum value in the array, as well as its index, but here is the output that I get

 -1 and 5 

The maximum value is returned in order, but not sure what is wrong with the index. This should really print 2 , but this is print -1 , please tell me where I am wrong and how I can fix it.

Thankd

+5
source share
4 answers

You must update the maximum 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; } } 

Reason Arrays.asList(num).indexOf(maxValue); returns -1 is that the array of primitives is converted by Arrays.asList to a List one element (the array itself) and that List does not contain maxValue > (it contains only the original array).

+21
source

It is necessary to update the index during iteration, 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 an example that @Eran refers to.

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

According to Javadoc, indexOf

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

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

enter image description here

+6
source

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.

+4
source

the answers are above 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
source

Source: https://habr.com/ru/post/1237639/


All Articles