简体   繁体   English

数组索引打印错误值

[英]Array index printing wrong value

I've the below Java code. 我有以下Java代码。

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. 这实际上应该打印2 ,但它打印-1 ,请让我知道我哪里出错了,我该如何解决这个问题。

Thankd Thankd

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); 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). 返回-1是一个基元数组由Arrays.asList转换为单个元素的List (数组本身),并且该List不包含maxValue (它只包含原始数组)。

Need to update index while iterating, getMaxIndex = i; 需要在迭代时更新索引, 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. 以下是@Eran所指的内容。

It is converted to List of size 1 , containing a single element (the array itself). 它被转换为size 1 List ,包含一个元素(数组本身)。

As per Javadoc, indexOf 按照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. 返回此列表中第一次出现的指定元素的索引,如果此列表不包含该元素,则返回-1。

So it searches for maxValue inside List and not inside array stored in 0th index of List . 因此它inside List搜索maxValuenot inside array stored in 0th index of List

在此输入图像描述

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. Arrays.asList()使用签名public static <T> List<T> asList(T... a) ,它接受可变数量的对象或仅占用对象数组。

However, int is a primitive type and not an object type. 但是, int是基本类型而不是对象类型。 So Arrays.asList(num) is not interpreted as "take this array", but as "take this object as one object". 因此Arrays.asList(num)不会被解释为“取这个数组”,而是“将此对象作为一个对象”。 The result is thus a List<int[]> , where the given number (of course) can't be found. 结果是List<int[]> ,其中找不到给定的数字(当然)。

So it is better to keep the index while searching for the maximum, as the other answers already suggest. 因此,最好在搜索最大值时保持索引,正如其他答案已经建议的那样。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM