客户端app版本比较代码实现

  • 客户端app版本比较代码实现已关闭评论
  • 82 views
  • A+
所属分类:未分类
/**
 *
 * @param version1
 * @param version2
 * @return
 *  version1>version2 返回1
 * = 0
 * < -1
 */
public  int compareVersion(String version1, String version2) {
    int len1 = version1.length();
    int len2 = version2.length();
    int start1 = 0;
    int start2 = 0;
    int[] nums = new int[2];
    while (start1 < len1 && start2 < len2) {
        nums = getNum(version1, start1, len1);
        int num1 = nums[1];
        start1 = nums[0];
        nums = getNum(version2, start2, len2);
        int num2 = nums[1];
        start2 = nums[0];
        if (num1 > num2) {
            return 1;
        } else if (num1 < num2) {
            return -1;
        }
    }
    if (start1 >= len1 && start1 >= len2) {
        return 0;
    } else if (start1 >= len1) {
        while (start2 < len2) {
            nums = getNum(version2, start2, len2);
            if (nums[1] != 0) {
                return -1;
            }
            start2 = nums[0];
        }
        return 0;
    } else {
        while (start1 < len1) {
            nums = getNum(version1, start1, len1);
            if (nums[1] != 0) {
                return 1;
            }
            start1 = nums[0];
        }
        return 0;
    }
}

public static int[] getNum(String str, int start, int len) {
    int end = start;
    int[] result = new int[2];
    while (end < len && str.charAt(end) != '.') {
        end++;
    }
    result[0] = end + 1;
    if (end == start) {
        result[1] = 0;
    } else {
        result[1] = Integer.valueOf(str.substring(start, end));
    }
    return result;
}
  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar