加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java操作数组的常用算法总结

发布时间:2020-12-15 00:11:20 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 public class BaseAlgorithm { public static void main(String[] args) { int[] arr = { 1,7,3,30,55,21,5,1,3 }; System.out.println(findMaxValue(

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

public class BaseAlgorithm {  
  
    public static void main(String[] args) {  
        int[] arr = { 1,7,3,30,55,21,5,1,3 };  
        System.out.println(findMaxValue(arr));  
        System.out.println(findMostValue(arr));  
        System.out.println(findMaxLen(arr));  
    }  
  
    /** 
     * 找出数组中最大元素 
     */  
    public static int findMaxValue(int[] arr) {  
        int max = arr[0];  
        for (int i = 1; i < arr.length; i++) {  
            if (arr[i] > max) {  
                max = arr[i];  
            }  
        }  
        return max;  
    }  
  
    /** 
     * 找出数组中重复出现最多的元素 
     */  
    public static int findMostValue(int[] arr) {  
        // 出现最多次数  
        int most = 0;  
        // 出现最多次数的数组下标  
        int index = 0;  
        for (int i = 0; i < arr.length; i++) {  
            int temp = 1;  
            for (int j = i; j < arr.length; j++) {  
                if (arr[i] == arr[j]) {  
                    temp++;  
                }  
            }  
            if (temp > most) {  
                most++;  
                index = i;  
            }  
        }  
        System.out.println("最多出现了" + most + "次");  
        return arr[index];  
    }  
  
    /** 
     * 找出最长连续递增序列的长度 
     */  
    public static int findMaxLen(int[] arr) {  
        int maxLen = 1;  
        int temp = 1;  
        for (int i = 0; i < arr.length - 1; i++) {  
            if (arr[i + 1] >= arr[i]) {  
                temp++;  
            } else {  
                if (temp > maxLen) {  
                    maxLen = temp;  
                }  
                temp = 1;  
            }  
        }  
        return maxLen;  
    }  
  
}  

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读