查找数组最大值
public class Test {
public static void main(String[] args) {
// 找出数组中的最大值,并记录它的下标
// 定义数组
int[] num_array = {1, 2, 12, 4, 5, 6, 9};
// 把数组0号数据放到temp区
int temp = num_array[0];
int temp_idx = 0;
// 遍历数组的操作
for (int i = 1; i <= num_array.length - 1; i++) {
System.out.println(i);
// 当前下标数据与临时区进行比较,谁大,换谁
if (num_array[i] > temp) {
temp = num_array[i];
temp_idx = i;
}
}
System.out.println("最大的数据" + temp);
System.out.println("最大数据的下标" + temp_idx);
}
}