注意點:
java split 分割'.' 要進行轉義java
public int compareVersion(String version1, String version2) { String[] sv1 = version1.split("\\."); String[] sv2 = version2.split("\\."); int[] iv1 = new int[sv1.length]; int[] iv2 = new int[sv2.length]; for (int i = 0; i < sv1.length; i++) iv1[i] = Integer.parseInt(sv1[i]); for (int i = 0; i< sv2.length; i++) iv2[i] = Integer.parseInt(sv2[i]); int i = 0, j = 0; while (i < iv1.length && j < iv2.length) if (iv1[i] < iv2[j]) return -1; else if (iv1[i] > iv2[j]) return 1; else{ i++; j++; } while (i < iv1.length) if (iv1[i] != 0) return 1; else i++; while (j < iv2.length) if (iv2[j] != 0) return -1; else j++; return 0; }