我应该编写一个程序来查找未排序的整数数组中的最大索引。
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.
所以我编写了一个返回最高值的程序,例如它返回 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;
}
}
最佳答案
也可以用,
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);
关于Java 数组返回最高数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21692659/