是否有比较版本号的标准习语?我不能直接使用String compareTo,因为我还不知道点释放的最大数量是多少。我需要比较版本,并有以下保持正确:
1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10
是否有比较版本号的标准习语?我不能直接使用String compareTo,因为我还不知道点释放的最大数量是多少。我需要比较版本,并有以下保持正确:
1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10
当前回答
public static void main(String[] args) {
String version1 = "1.0";
String version2 = "1.0.0";
String[] version1_splits = version1.split("\\.");
String[] version2_splits = version2.split("\\.");
int length = version1_splits.length >= version2_splits.length ? version1_splits.length : version2_splits.length;
int i=0;
for(;i<length;i++){
int version1_int = getValue(version1_splits,i);
int version2_int = getValue(version2_splits,i);
if(version1_int > version2_int){
System.out.println("version1 > version2");
break;
}
else if(version1_int < version2_int){
System.out.println("version2 > version1");
break;
}
else{
if(i == length-1)
System.out.println("version1 = version2");
}
}
}
private static int getValue(String[] version1_splits, int i) {
int temp;
try{
temp = Integer.valueOf(version1_splits[i]);
}
catch(IndexOutOfBoundsException e){
temp=0;
}
return temp;
}
其他回答
使用Java 8 Stream替换组件中的前导零。这段代码通过了interviewbit.com上的所有测试
public int compareVersion(String A, String B) {
List<String> strList1 = Arrays.stream(A.split("\\."))
.map(s -> s.replaceAll("^0+(?!$)", ""))
.collect(Collectors.toList());
List<String> strList2 = Arrays.stream(B.split("\\."))
.map(s -> s.replaceAll("^0+(?!$)", ""))
.collect(Collectors.toList());
int len1 = strList1.size();
int len2 = strList2.size();
int i = 0;
while(i < len1 && i < len2){
if (strList1.get(i).length() > strList2.get(i).length()) return 1;
if (strList1.get(i).length() < strList2.get(i).length()) return -1;
int result = new Long(strList1.get(i)).compareTo(new Long(strList2.get(i)));
if (result != 0) return result;
i++;
}
while (i < len1){
if (!strList1.get(i++).equals("0")) return 1;
}
while (i < len2){
if (!strList2.get(i++).equals("0")) return -1;
}
return 0;
}
我现在就做了,然后问自己,这对吗?因为我从来没有找到过比我的更干净的解决方案
你只需要像下面这样拆分字符串版本("1.0.0"):
userVersion.split("\\.")
那么你将得到:{"1","0","0"}
现在,用我做过的方法
isUpdateAvailable(userVersion.split("\\."), latestVersionSplit.split("\\."));
方法:
/**
* Compare two versions
*
* @param userVersionSplit - User string array with major, minor and patch version from user (exemple: {"5", "2", "70"})
* @param latestVersionSplit - Latest string array with major, minor and patch version from api (example: {"5", "2", "71"})
* @return true if user version is smaller than latest version
*/
public static boolean isUpdateAvailable(String[] userVersionSplit, String[] latestVersionSplit) {
try {
int majorUserVersion = Integer.parseInt(userVersionSplit[0]);
int minorUserVersion = Integer.parseInt(userVersionSplit[1]);
int patchUserVersion = Integer.parseInt(userVersionSplit[2]);
int majorLatestVersion = Integer.parseInt(latestVersionSplit[0]);
int minorLatestVersion = Integer.parseInt(latestVersionSplit[1]);
int patchLatestVersion = Integer.parseInt(latestVersionSplit[2]);
if (majorUserVersion <= majorLatestVersion) {
if (majorUserVersion < majorLatestVersion) {
return true;
} else {
if (minorUserVersion <= minorLatestVersion) {
if (minorUserVersion < minorLatestVersion) {
return true;
} else {
return patchUserVersion < patchLatestVersion;
}
}
}
}
} catch (Exception ignored) {
// Will be throw only if the versions pattern is different from "x.x.x" format
// Will return false at the end
}
return false;
}
等待任何反馈:)
我自己写了一个小函数。更简单地使用列表
public static boolean checkVersionUpdate(String olderVerison, String newVersion) {
if (olderVerison.length() == 0 || newVersion.length() == 0) {
return false;
}
List<String> newVerList = Arrays.asList(newVersion.split("\\."));
List<String> oldVerList = Arrays.asList(olderVerison.split("\\."));
int diff = newVerList.size() - oldVerList.size();
List<String> newList = new ArrayList<>();
if (diff > 0) {
newList.addAll(oldVerList);
for (int i = 0; i < diff; i++) {
newList.add("0");
}
return examineArray(newList, newVerList, diff);
} else if (diff < 0) {
newList.addAll(newVerList);
for (int i = 0; i < -diff; i++) {
newList.add("0");
}
return examineArray(oldVerList, newList, diff);
} else {
return examineArray(oldVerList, newVerList, diff);
}
}
public static boolean examineArray(List<String> oldList, List<String> newList, int diff) {
boolean newVersionGreater = false;
for (int i = 0; i < oldList.size(); i++) {
if (Integer.parseInt(newList.get(i)) > Integer.parseInt(oldList.get(i))) {
newVersionGreater = true;
break;
} else if (Integer.parseInt(newList.get(i)) < Integer.parseInt(oldList.get(i))) {
newVersionGreater = false;
break;
} else {
newVersionGreater = diff > 0;
}
}
return newVersionGreater;
}
我创建了一个简单的实用程序,使用语义版本约定在Android平台上比较版本。所以它只适用于X.Y.Z (Major.Minor.Patch)格式的字符串,其中X、Y和Z是非负整数。你可以在我的GitHub上找到它。
方法version . compareversions (String v1, String v2)比较两个版本字符串。如果版本相等则返回0,如果版本v1在版本v2之前则返回1,如果版本v1在版本v2之后则返回-1,如果版本格式无效则返回-2。
我喜欢@Peter Lawrey的想法,我把它扩展到更远的范围:
/**
* Normalize string array,
* Appends zeros if string from the array
* has length smaller than the maxLen.
**/
private String normalize(String[] split, int maxLen){
StringBuilder sb = new StringBuilder("");
for(String s : split) {
for(int i = 0; i<maxLen-s.length(); i++) sb.append('0');
sb.append(s);
}
return sb.toString();
}
/**
* Removes trailing zeros of the form '.00.0...00'
* (and does not remove zeros from, say, '4.1.100')
**/
public String removeTrailingZeros(String s){
int i = s.length()-1;
int k = s.length()-1;
while(i >= 0 && (s.charAt(i) == '.' || s.charAt(i) == '0')){
if(s.charAt(i) == '.') k = i-1;
i--;
}
return s.substring(0,k+1);
}
/**
* Compares two versions(works for alphabets too),
* Returns 1 if v1 > v2, returns 0 if v1 == v2,
* and returns -1 if v1 < v2.
**/
public int compareVersion(String v1, String v2) {
// Uncomment below two lines if for you, say, 4.1.0 is equal to 4.1
// v1 = removeTrailingZeros(v1);
// v2 = removeTrailingZeros(v2);
String[] splitv1 = v1.split("\\.");
String[] splitv2 = v2.split("\\.");
int maxLen = 0;
for(String str : splitv1) maxLen = Math.max(maxLen, str.length());
for(String str : splitv2) maxLen = Math.max(maxLen, str.length());
int cmp = normalize(splitv1, maxLen).compareTo(normalize(splitv2, maxLen));
return cmp > 0 ? 1 : (cmp < 0 ? -1 : 0);
}
希望它能帮助到别人。它通过了interviewbit和leetcode中的所有测试用例(需要取消compareVersion函数中的两行注释)。
很容易测试!