Java数组返回最高数组索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21692659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:05:45  来源:igfitidea点击:

Java arrays returning highest array index

javamax

提问by user3071909

I am supposed to write a program that finds the largest index in an unsorted array of integers.

我应该编写一个程序,在未排序的整数数组中找到最大的索引。

Use a method that takes an array of integers as a parameter. The method should search the array and return the index of the largest value.

使用将整数数组作为参数的方法。该方法应该搜索数组并返回最大值的索引。

So i wrote a program that returns the highest value, for example it returns 13 as the highest number instead of the index of 2. So how would i return the index instead of the highest number itself? If that is an easy fix, does the rest of my code look correct? Thanks!

所以我写了一个返回最高值的程序,例如它返回 13 作为最高数字而不是 2 的索引。那么我将如何返回索引而不是最高数字本身?如果这是一个简单的修复,我的其余代码看起来是否正确?谢谢!

public class LargestInteger 
{
    public static void main(String[] args)
    {
        int[] largeArray = {5,4,13,7,7,8,9,10,5};

        System.out.println(findLargest(largeArray));
    }

    public static int findLargest(int array[])
    {
        int largest = array[0];

        for(int i = 0; i < array.length; i++)
        {
            if(array[i] > largest)
                largest = array[i];   
        }

        return largest;
    }
}

回答by Jason

You need to keep and store the largest index. Try this:

您需要保留和存储最大的索引。尝试这个:

public static int findLargest(int array[])
{
    int largest = array[0];
    int largestIndex = 0;

    for(int i = 0; i < array.length; i++)
    {
        if(array[i] > largest) {
            largest = array[i]; 
            largestIndex =i;
        }  
    }

    return largestIndex;
}

回答by Arbiter

Create a forloop at the end of the method, and a variable like index. Search through the list and find if the variable matches, remembering to increment the indexvariable every time.

for在方法末尾创建一个循环,并创建一个变量,如index. 搜索列表并查找变量是否匹配,记住index每次都增加变量。

Example:

例子:

int index = 0;
for (int i : array)
{
    if (i == largest)
    {
         break;
    }
    index++;
}
return index;

回答by msmolcic

You can add aditional method that gets largest number and an array then search for index of that number:

您可以添加获取最大数字和数组的附加方法,然后搜索该数字的索引:

public class LargestInteger 
{
    public static void main(String[] args)
    {
        int[] largeArray = {5,4,13,7,7,8,9,10,5};

        System.out.println(indexOfLargest(largeArray, findLargest(largeArray)));
    }

    public static int findLargest(int array[])
    {
        int largest = array[0];

        for(int i = 0; i < array.length; i++)
        {
            if(array[i] > largest)
                largest = array[i];   
        }

        return largest;
    }

    public static int indexOfLargest(int array[], int number)
    {

        for (int i = 0; i < array.length; i++) {
            if (array[i] == number)
                return i;            
        }

        return -1;
    }

}

回答by Rakesh KR

Can also done with,

也可以做到,

int[] largeArray     = {5,4,13,7,7,8,9,10,5};
int   largestElement = findLargest(largeArray);
int   index          = Arrays.asList(5,4,13,7,7,8,9,10,5).indexOf(largestElement);

回答by Gabriel

In java 8 you can do it simply:

在 java 8 中,你可以简单地做到:

To get the highest:

获得最高:

int[] largeArray = {5,4,13,7,7,8,9,10,5};
System.out.println(IntStream.of(largeArray).max().getAsInt());

result: 13

结果:13

And to sum them:

总结一下:

System.out.println(IntStream.of(largeArray).sum());

result: 68

结果:68