Java Implementation Android Client version number comparison

Source: Internet
Author: User

Catalogue

    • Directory
    • Objective
    • Key points
    • Java Code Implementation

Preface

In the morning, I looked at CSDN blog rankings, the highest ranking article of the week is: Java implementation comparison version number. Personally think this article is good, because the Android client system often use version number comparison function, especially in the case of application self-upgrade.
The original blogger wrote this article because the majority of Java programmers might take the CompareTo API call or the irregular version number name.
And I write this article, because of the ACM topic and add to the original blogger code is not considered well-rounded situation.

Key points

Here, there are two questions to illustrate the key points of the client version number comparison.

Why can't I use the String.CompareTo method to compare the client version number ?
For a chestnut, before the client version number is: 9.9.9, and the server side of the latest client version number is: 10.0.1, although 10.0.1 significantly higher than 9.9.9, but according to the CompareTo method, this 9.9.9 is greater than 10.0.1, resulting in a client version number comparison error.

What considerations are missing from the original blogger code?
The original blogger considered 8.5c, a version number that is unlikely to be defined, but did not consider the case of leading 0 in the version number. For example, according to the original blogger's code, version number 1.0 is greater than the version number 1.

Why are ACM topics respected?
The reason why we recommend you do ACM, is because I first think of this kind of problem is through Leetcode on a difficult to easy string comparison topic, the topic Link: Compare Version number. And the original blogger's code is unable to ac this problem.

Java Code Implementation

Pull some, and finally to show the code, the following is my client version number comparison code, welcome to the slot.

     Public Static int compareversion(String version1, String version2) {if(Version1.equals (Version2)) {return 0; } string[] Version1array = Version1.split ("\\."); string[] Version2array = Version2.split ("\\.");intindex =0;intMinlen = Math.min (Version1array.length, version2array.length);intdiff =0; while(Index < Minlen && (diff = integer.parseint (version1array[index))-Integer.parseint (Version2array[index])) ==0) {index + +; }if(diff = =0) { for(inti = index; i < version1array.length; i + +) {if(Integer.parseint (Version1array[i]) >0) {return 1; }            } for(inti = index; i < version2array.length; i + +) {if(Integer.parseint (Version2array[i]) >0) {return-1; }            }return 0; }Else{returnDiff >0?1: -1; }    }

Java Implementation Android Client version number comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.