1

Using Java, I want to split an array of ints into an array of ranges. For example, the array

[1, 2, 3, 4, 5, 6, 7, 8, 100, 101, 102, 103]

would return

[Range(1, 8), Range(100, 103)]

because it consists of 1 to 8 and 100 to 103

It would be best if there were a library, but any sort of method of this conversion is fine.

2
  • 1
    where is 2 from old array??? if you want random elements from your array then you should write your own method. Commented May 23, 2015 at 16:16
  • i didn't downvote your question. and how you are pretty sure that i have downvote???? Commented May 23, 2015 at 21:51

2 Answers 2

2

Ok James, here's your working solution :)

private void executeTestCode(){
    int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 100, 101, 102, 103};      
    Range[] ranges = Arrays.stream(getRanges(arr)).filter(r -> r != null).toArray(Range[]::new);        
    String output = Arrays.stream(ranges)
            .map(Range::toString)
            .reduce("[", (dynamicString, stringedRange) -> (dynamicString + stringedRange + ", "));
    output = output.substring(0, output.length()-2) + "]";
    System.out.println(output);
}

OUTPUT:
[Range(1, 8), Range(100, 103)]

private Range[] getRanges(int[] toBeProcessed){
    Range[] result = new Range[toBeProcessed.length]; //larger array won't be needed
    int startRange = toBeProcessed[0];  
    int ranges = 0;
    for(int a=0; a<toBeProcessed.length; a++){
        try{
            if(toBeProcessed[a] + 1 != toBeProcessed[a+1]){                 
                result[ranges] = new Range(startRange, toBeProcessed[a]);
                startRange = toBeProcessed[a+1];
                ranges++;
            }                   
        }catch(ArrayIndexOutOfBoundsException e){
            result[ranges] = new Range(startRange, toBeProcessed[toBeProcessed.length-1]);
        }
    }
    return result;      
}



CLASS Range:

class Range{
    private int start, end;
    public Range(int st, int en){
        start = st;
        end = en;
    }

    @Override
    public String toString(){
        return "Range(" +start+ ", " +end+ ")";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'd give this an upvote, but this is almost certainly a homework question. :-) For things that look like homework, it's (sometimes) favored to give hints and not the full answer. That said, good code.
1

You can use

Arrays.copyOfRange(yourArray, inclusiveStartIndex, exclusiveEndIndex);

It returns new array.

2 Comments

Why isn't it what you want? That seems like what you asked for here, so it's hard to suggest something else until folks know why this doesn't work. :)
More specifically, arrays in Java are immutable; they can't be changed once created. (The values in them can, but the array itself - including it's length - is permanent.) There's not a way to split arrays without at least doing a shallow copy of primatives stored in them. (If your values were Integer instead of int, a shallow copy could use existing Objects.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.