package com.charsming.wysk.utils;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ArrayUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* @ClassName: ToInterval
* @Description:
* @Author: charsming
* @Date: 2023-05-24 15:43
*/
public class ToInterval {
public static String ToIntervalSort(List<Integer> list) {
if (list.isEmpty()) {
return "--";
}
//排序
Collections.sort(list);
StringBuffer stringBuffer = new StringBuffer();
//当前值
for (int i = 0; i < list.size() - 1; i++) {
//当前值加1不等于后一个索引,不连续
if (list.get(i) + 1 != list.get(i + 1)) {
stringBuffer.append(list.get(i).toString() + ",");
} else {
//如果连续
int length = stringBuffer.toString().length();
if (length == 0) {
stringBuffer.append(list.get(i) + "-");
} else {
String substring = stringBuffer.substring(length - 1);
if (!substring.equals("-")) {
stringBuffer.append(list.get(i) + "-");
}
}
}
}
stringBuffer.append(list.get(list.size() - 1));
return stringBuffer.toString();
}
}
数字字符串转换为区间表示
于 2023-05-24 15:39:45 首次发布