显示数组中的最高值和索引号

Joh*_*Joe 2 java arrays

我有一个方法,用于显示最高值,并显示它所属的索引号.到目前为止,它已经可以显示最高值,但索引号无法显示.我该怎么办才能让系统显示i价值呢?

private void pick_highest_value_here_and_display(ArrayList<Double> value) throws Exception {
    // TODO Auto-generated method stub
    double aa[]=value.stream().mapToDouble(v -> v.doubleValue()).toArray(); 
    double highest=Double.MIN_VALUE;

    System.out.println(highest);
    for(int i=0;i<aa.length;i++)
    {
        if(aa[i]>highest)
        {
            highest=aa[i];
        }
    }
    System.out.println(highest);
    System.out.println(i); // Error: create local variable i
}
Run Code Online (Sandbox Code Playgroud)

Erw*_* C. 8

您只需修改代码即可保存最大AND i:

private void pick_highest_value_here_and_display(ArrayList<Double> value) throws Exception {
    // TODO Auto-generated method stub
    double aa[]=value.stream().mapToDouble(v -> v.doubleValue()).toArray(); 
    double highest=Double.MIN_VALUE;
    int index=0;
    System.out.println(highest);
    for(int i=0;i<aa.length;i++)
    {
        if(aa[i]>highest)
        {
            index=i;
            highest=aa[i];
        }
    }
    System.out.println(highest);
    System.out.println(index); 
}
Run Code Online (Sandbox Code Playgroud)