Fill arrays with ranges of numbers

Viewed 97882

Is there any syntax/package allowing quick filling of java arrays with ranges of numbers, like in perl?

e.g.

int[] arr = new int[1000];
arr=(1..500,301..400,1001..1400); // returns [1,2,3,4,...,500,301,302,...,400,1001,1002,...1400]

Also, it here a package that allows getting the n-th number in such list of numbers as the above, without actually creating the array (which can be huge)?

e.g.

BunchOfRangesType bort = new BunchOfRangesType("1..500","301..400","1001..1400");
bort.get(0); // return 1
bort.get(500); // return 301
bort.get(501); // return 302

It's not too difficult to implement, but I guess it might be common so maybe it was already done.

7 Answers
 public static int[] getRangeArray(int start, int end){
        final int[] rangeArray = new int[end-start + 1];
            for(int i = 0; i < rangeArray.length; i++){
                rangeArray[i] = start + i;
          }
       return rangeArray; // It includes start and end values as well, pls adjust according to your need
  }
Related