JAVA中获取数组中的最值

来源:互联网 发布:南通网络营销seo 编辑:程序博客网 时间:2024/06/10 06:20

JAVA中获取数组中的最值

/** * *Title: project_name *Description:数组 *Makedate:2016年5月1日 下午1:22:44*@author: sunt Email:wnst1990@126.com *@version: 1.0 */public class Demo {    public static void main(String[] args) {        /**         * 1.定义数组并遍历数组         */        int arr [] = {12,34,345,54,65,6,75};        for (int i = 0; i < arr.length; i++) {            System.out.println("arr[" + i +"] = " + arr[i]);        }        /**         * 定义一个方法,获取数组中的最大值         */        int maxArr = MaxArr(arr);        System.out.println("数组中的最大值 = " + maxArr);    }    public static int MaxArr(int [] arr){        int max = arr[0];        for (int i = 0; i < arr.length; i++) {            if(arr[i] > max){                max = arr[i];            }        }        return max;    }}
0 0
原创粉丝点击