/**
* 通过索引获取最大值
*
*
*/
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);
        }
        //通过假定数组中的第一个元素是最大值,然后不断的进行判断,最终获取对大值
        public static int getMax(int[] arr){
                int max = arr[0];//假设数组的第一个元素为最大值
                for(int i=0;i<arr.length;i++) {
                        if(max<arr[i]) {
                                max = arr[i];
                        }
                }
                return max;
        }
        /**
         * 此方法可以求出数组对应的最大值或者最小值
         * @param arr
         * @param flag :true:表示求最大值,false:表示求最小值
         * @return
         */
        public static int getMax(int[] arr,boolean flag){
                int max = arr[0];//假设数组的第一个元素为最值
                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;
        }
        
        //通过获取最大值的角标,最终返回该角标对应的数值
        public static int getMaxByIndex(int[] arr){
                int max = 0;//假设数组的角标为0的元素是最大的
                for(int i=0;i<arr.length;i++) {
                        if(arr[max]<arr[i]) {
                                max = i; //max中存储的是当前最大值所对应的角标
                        }
                }
                return arr[max];
        }
        
}

最新文章

  1. Struts2使用demo
  2. C#读取XML文件并取值
  3. TinyFrame升级之十:WCF Rest Service注入IOC的心
  4. How to install the zsh shell in Linux &amp;&amp; how to set it as a default login shell
  5. ORACLE 数据块dump
  6. paip. 解决java程序不能自动退出
  7. poj 2253 Frogger dijkstra算法实现
  8. node.js 1Million concurrent connections!
  9. TCP 函数
  10. javaWeb实现使用邮箱邮件找回密码功能
  11. 【转载】cocos2d-x2.2.3和android的平台环境
  12. 几道数位DP
  13. ASP.NET Core WebApi AspNetCoreRateLimit 限流中间件学习
  14. Linux内核源码分析--内核启动之(2)Image内核启动(汇编部分)(Linux-3.0 ARMv7) 【转】
  15. centos 7 版本升级nginx
  16. python入门-用户输入
  17. Spring Tools Suite (STS) 简介
  18. 微信小程序如何转换成分与秒
  19. Eclipse中的BuildPath详解【转载】
  20. IOS 开发之--获取真机的deviceToeken

热门文章

  1. 比 git log 更强大的 git reflog
  2. MySQL迁移到SQLServer
  3. 更改Mysql登录密码
  4. C 编程中fseek、ftell的用法总结
  5. cocos2dx笔记1:概述
  6. oracle安装登录sqlplus / as sysdba然后报错ERROR: ORA-01031 insufficient privileges
  7. CRM IFD 部署在同一台服务器上遇到的错误
  8. http_load常见问题及解决方案
  9. Gerrit代码Review实战
  10. css3 all属性