• 比较APP版本号大小


    	public static void main(String[] args) {
    		String[] versions = { "1.9", "1.10", "1.0" };
    		for (int i = versions.length - 1; i > 0; i--) {
    			for (int j = 0; j < i; j++) {
    				if (compareAppVersion(versions[i], versions[j]) > 0) {
    					String temp = versions[j];
    					versions[j] = versions[j + 1];
    					versions[j + 1] = temp;
    				}
    			}
    		}
    		System.out.println(JSON.toJSONString(versions));
    	}
    
    	/**
    	 * 比较APP版本号的大小
    	 * 1、前者大则返回一个正数
    	 * 2、后者大返回一个负数
    	 * 3、相等则返回0
    	 */
    	public static int compareAppVersion(String version1, String version2) {
            if (version1 == null || version2 == null) {
                throw new RuntimeException("版本号不能为空");
            }
            // 注意此处为正则匹配,不能用.
            String[] versionArray1 = version1.split("\.");
            String[] versionArray2 = version2.split("\.");
            int idx = 0;
            // 取数组最小长度值
            int minLength = Math.min(versionArray1.length, versionArray2.length);
            int diff = 0;
            // 先比较长度,再比较字符
            while (idx < minLength
                    && (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0
                    && (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {
                ++idx;
            }
            // 如果已经分出大小,则直接返回,如果未分出大小,则再比较位数,有子版本的为大
            diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
            return diff;
        }
    

      

  • 相关阅读:
    redis集群规范
    mongodb的基本使用
    redis进阶
    redis基本使用
    selenium的基本使用
    C++入门
    C语言入门
    MATLAB中矩阵reshape的顺序规律
    Tensorflow:ImportError: DLL load failed: 找不到指定的模块 Failed to load the native TensorFlow runtime
    差分定位和精密定位
  • 原文地址:https://www.cnblogs.com/a5513633/p/13931546.html
Copyright © 2020-2023  润新知