找出数组中最大值and索引

找出数组中的最大值和和最大值的索引位置。。。。。

第一中方法:
/** * 找出数组中最大值和最大值的索引 * @param args */ public static void main(String[] args) { int[] arr = {10, 4, 1, 2, 1, 3, 789,4, 5,89, 6, 7}; int temp = arr[0]; int index = 0; for (int i = 1; i < arr.length; i++) { index = temp > arr[i] ? index : i; temp = temp > arr[i] ? temp : arr[i]; } System.out.println(index); System.out.println(temp); }
第二种方法:
  int a[] = {10, 15, 6, 100, 89,4662, 88, 45};
      int b = 0;
      int c = 0;
    for (int i = 0; i < a.length; i++) {
        b = a[i] > b ? a[i]:b ;
        if (b==a[i]){
            c=i;
        }
    }
    System.out.println(b);
    System.out.println(c);

猜你喜欢

转载自www.cnblogs.com/axu521/p/10473488.html
今日推荐