Java List Concatenate concatIntegersToRanges( List damages)

Here you can find the source of concatIntegersToRanges( List damages)

Description

concat Integers To Ranges

License

LGPL

Declaration

public static ArrayList<int[]> concatIntegersToRanges(
            List<Integer> damages) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.*;

public class Main {
    public static ArrayList<int[]> concatIntegersToRanges(
            List<Integer> damages) {
        ArrayList<int[]> ranges = new ArrayList<int[]>();
        if (damages.size() == 0)
            return ranges;

        Collections.sort(damages);
        int start = -1;
        int next = 0;
        for (Integer i : damages) {
            if (start == -1) {
                start = next = i;//from  w  w  w. j av  a2s. c om
                continue;
            }
            if (next + 1 != i) {
                ranges.add(new int[] { start, next });
                start = next = i;
                continue;
            }
            next = i;
        }
        ranges.add(new int[] { start, next });
        return ranges;
    }
}

Related

  1. concatenateParameters(List parameters)
  2. concatenateStrings(List strings, String separator)
  3. concatenateUniqueLists(List first, List second)
  4. concatGenericList(List list, String delimiter)
  5. concatinateStrings(List strList, String delim)
  6. concatLines(List lines)
  7. concatLines(List lines)
  8. concatLineSeparated(List discountCommentCauseList)
  9. concatList(List list)