溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

AJPFX中怎么通過索引獲取最大值

發(fā)布時間:2021-07-29 14:42:15 來源:億速云 閱讀:139 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關(guān)AJPFX中怎么通過索引獲取最大值,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

/**
* 通過索引獲取最大值
*/
public class Test1 {
        public static void main(String[] args) {
                int[] arr = {11,22,33,44,55,66};
                int max = getMax(arr);
                System.out.println(max);
                System.out.println("=======================");
                int max2 = getMaxByIndex(arr);
                System.out.println(max2);
                
                System.out.println("----------------------");
                int max3 = getMax(arr, true);
                System.out.println(max3);
                System.out.println("**************************");
                int max4 = getMax(arr,false);
                System.out.println(max4);
        }
        //通過假定數(shù)組中的第一個元素是最大值,然后不斷的進行判斷,最終獲取對大值
        public static int getMax(int[] arr){
                int max = arr[0];//假設數(shù)組的第一個元素為最大值
                for(int i=0;i<arr.length;i++) {
                        if(max<arr[i]) {
                                max = arr[i];
                        }
                }
                return max;
        }
        /**
         * 此方法可以求出數(shù)組對應的最大值或者最小值
         * @param arr
         * @param flag :true:表示求最大值,false:表示求最小值
         * @return
         */
        public static int getMax(int[] arr,boolean flag){
                int max = arr[0];//假設數(shù)組的第一個元素為最值
                for(int i=0;i<arr.length;i++) {
                        if(flag) {
                                if(max<arr[i]) {
                                        max = arr[i];
                                }                                
                        } else {
                                if(max>arr[i]) {
                                        max = arr[i];
                                }
                        }
                }
                return max;
        }
        
        //通過獲取最大值的角標,最終返回該角標對應的數(shù)值
        public static int getMaxByIndex(int[] arr){
                int max = 0;//假設數(shù)組的角標為0的元素是最大的
                for(int i=0;i<arr.length;i++) {
                        if(arr[max]<arr[i]) {
                                max = i; //max中存儲的是當前最大值所對應的角標
                        }
                }
                return arr[max];
        }
        
}

以上就是AJPFX中怎么通過索引獲取最大值,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI