主要是来做合并排序的,转过来,以后也好用
public class Main {
public static void main(String[] args) {
Integer[] NoNum = {1, 2, 3, 5, 7, 8, 9, 10, 13};
String result = convert(NoNum, 0);
System.out.println(result.substring(0, result.length() - 1));
}
public static String convert(Integer[] ints, int index) {
int end = index;
if (ints.length == index) {//结束条件,遍历完数组
return "";
} else {
for (int i = index; i < ints.length; i++) {
if (i < ints.length - 1) {
if (ints[i] + 1 == ints[i + 1]) {
end = i;
} else {
if (i > index)
end = end + 1;
break;
}
} else {
if (end == ints.length - 2) {
end = ints.length - 1;
break;
}
}
}
if (index == end)//相等说明不连续
return ints[index] + "," + convert(ints, end + 1);
else//连续
return ints[index] + "-" + ints[end] + "," + convert(ints, end + 1);
}
}
}