28,29,30,35,36,37,87,88,101,102,103,104,105,106
以上是一串数字,数字连续在一起的分成一组,共有4组
请教高手如何分组成(28,29,30) (35,36,37) (87,88) (101,102,103,104,105,106)这样4组,利用java实现。
public class GroupNumberSplitter {
public static void main(String[] args) {
int[] intDataArray = new int[]{28, 29, 30, 35, 36, 37, 87, 88, 101, 102, 103, 104, 105, 106};
List<List<Integer>> result = split(intDataArray);
System.out.println(result.size());
}
private static List<List<Integer>> split(int[] intDataArray) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> group = null;
for (int intValue : intDataArray) {
if (group == null || group.get(group.size() - 1) + 1 != intValue) {
group = new ArrayList<Integer>();
result.add(group);
}
group.add(intValue);
}
return result;
}
}