java数组中取出最大值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java数组中取出最大值相关的知识,希望对你有一定的参考价值。
1 class Demo{ 2 public static void main(String []args){ 3 int[] arr={3,54,456,342,2798}; 4 int max=getMax(arr); 5 System.out.print("max="+max);//max=2798 6 } 7 8 /** 9 * 取出数组中的最大值 10 * @param arr 11 * @return 12 */ 13 public static int getMax(int[] arr){ 14 int max=arr[0]; 15 for(int i=1;i<arr.length;i++){ 16 if(arr[i]>max){ 17 max=arr[i]; 18 } 19 } 20 return max; 21 } 22 }
以上是关于java数组中取出最大值的主要内容,如果未能解决你的问题,请参考以下文章