public static void main(String[] args) {
int[] arr = new int[]{1,2,3,4,5,111,333,2,77,6,-1};
int index = getMax(arr);
System.out.println(MessageFormat.format("最大值:{0},索引:{1}",arr[index],index));
}
public static int getMax(int[] arr){
int k = arr[0];
int index = 0;
for (int i = 1; i < arr.length; i++) {
if(k<arr[i]){
k = arr[i];
index = i;
}
}
return index;
} |