Fill arrays with ranges of numbers

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


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)?


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.

-------------Problems Reply------------

Not quite as clean as True Soft's answer, but you can use Google Guava to the same effect:

public class Test {

public static void main(String[] args) {
//one liner
int[] array = toArray(newLinkedList(concat(range(1, 10), range(500, 1000))));

//more readable
Iterable<Integer> values = concat(range(1, 10), range(500, 1000));
List<Integer> list = newLinkedList(values);
int[] array = toArray(list);


public static List<Integer> range(int min, int max) {
List<Integer> list = newLinkedList();
for (int i = min; i <= max; i++) {

return list;


Note you need a few static imports for this to work.

There is dollar:

// build the List 10, 11, 12, 13, 14
List<Integer> list2 = $(10, 15).toList();



As for the first question, whether it is possible to fill an array with the values of a range: it is actually possible to achieve that with the combination of Range, DiscreteDomain, ContiguousSet and Ints from Guava:

int[] array = Ints.toArray(
ContiguousSet.create(Range.closed(1, 500), DiscreteDomain.integers()));

And, not exactly what is mentioned in the second part of the question, but it is possible to create a set with the elements of a range of a discrete domain:

Set<Integer> numbersFrom1To500 =
ContiguousSet.create(Range.closed(1, 500), DiscreteDomain.integers());

The resulting Set will not contain the specified elements physically, only logically (so it's memory footprint will be small), but can be iterated (since it's a Set):

for (Integer integer : numbersFrom1To500) {

Category:java Views:0 Time:2010-08-02
Tags: java arrays

Related post

  • How to convert sequence of numbers in an array to range of numbers 2010-02-16

    In javascript how to convert sequence of numbers in an array to range of numbers? eg. [2,3,4,5,10,18,19,20] to [2-5,10,18-20] --------------Solutions------------- Here is an algorithm that I made some time ago, originally written for C#, now I ported

  • Fill array with binary numbers 2010-05-08

    First of all this is not homework! My question is from the book: Algorithms in C++ third edition by Robert Sedgewick. There is given an array of size n by 2^n (two dimensional) and we should fill it with binary numbers of bits size exactly n. For exa

  • how can i call a range of numbers in an array? 2011-09-22

    So im wanting to compare a number(pullNumb)(gotten by another function elsewhere) to a range of numbers in an array. for this example, lets say: var pullNumb = 100; var morn_T= new Array(6,7,8,9,10,11,12,13,14,15,16,17,18,19); if(pullNumb>morn_T[1

  • Initialize an array of int with a range of numbers 2012-03-27

    I want to initialize an array of int and populate it with a range of numbers: return new int[].Populate(30,50); So then I would have an array with 30, 31, 32, 33... - 50 --------------Solutions------------- You can use Enumerable.Range() to e.g. retu

  • Filling array with numbers 2010-05-19

    I have such situation: There is 8 div-blocks with ids like 'rateN_wrapper' where is 'N' is the number of div: <div id="rate1_wrapper"> <a href="#" id="0_1">...</a> <a href="#" id="0_2">...</a> <a href="#" id="0_3">

  • How can I concatenate ranges of numbers into an array in MATLAB? 2010-09-16

    For example, I want to combine two ranges of numbers like this: 1 2 3 4 5 11 12 13 14 15 16 17 18 19 20 So, I tried: a = 1:5,11:20 but that didn't work. I also want to do this in a non-hardcoded way so that the missing 5 elements can start at any ind

  • How do I fill arrays in Java? 2009-02-23

    I know how to do it normally, but I could swear that you could fill out out like a[0] = {0,0,0,0}; How do you do it that way? I did try Google, but I didn't get anything helpful. --------------Solutions------------- You can also do it as part of the

  • Calculations of ranges of numbers in PHP 2009-11-29

    and first of all, thank you for taking the time to read my question. I am trying to write a script, and I've come across an issue which I am finding hard to solve. I am working with a pair of numbers (for example, 1000 and 2000), and I have an array

  • How to find if range is contained in an array of ranges? 2009-10-22

    Example business_hours['monday'] = [800..1200, 1300..1700] business_hours['tuesday'] = [900..1100, 1300..1700] ... I then have a bunch of events which occupy some of these intervals, for example event = { start_at: somedatetime, end_at: somedatetime

  • regex problem for newbie ranges of numbers 2010-09-13

    I really should polish up on my regex but for now can anyone help with this... ((2,3,4,11,8),(5,44,67,78,32,22,111,234)) as you can see, each range of numbers is comma separated and, in this example, there are 2 ranges of of numbers. In a live scenar

  • The fastest way to fetch data from SQL Server and fill an Excel range 2011-01-13

    I had to fill an Excel range from SQL Server according the following scheme C1 C2 C3....C29 L1 L2 L3 L4 L5 ..... L120 I wondered what could be the fastest way to fetch each value corresponding to each pair (Li,Cj), value which is stored in SqlServer

  • How to check if an integer is within a range of numbers in PHP? 2011-01-13

    How can I check if a given number is within a range of numbers? --------------Solutions------------- The expression: ($min <= $value) && ($value <= $max) will be true if $value is between $min and $max, inclusively See the PHP docs for

  • Permutations of multiple ranges of numbers 2011-01-26

    I need to generate permutations from multiple ranges of numbers in array. using namespace std; int generatePermutations(vector<int> &myVector, vector<vector<int> > &swappable) { int i = 0, s = 0; for (s = 0; s < swappable

  • Formula to bucket the range of numbers 2011-02-11

    I have over 10,000 lines of data, I would like to have the range starting from 50-10,000 For example 50-52 53-55 56-58 and etc. Any Excel formulas to bucket the range of numbers so that I will save up the trouble typing a few thousand lines for the r

  • Best programming language for very large arrays and very large numbers? 2011-02-17

    What would be the best programming language for very large arrays and very large numbers? With arrays over 30,000 indexes And numbers over 100 digits Also it needs to be efficient, or easy to make efficient. Thanks. --------------Solutions-----------

  • Interview puzzle: array size-n contains numbers from [i, i + n) 2011-03-24

    Given an unsorted array of numbers, write a function that returns true if array consists of consecutive numbers. Examples: If array is {5, 2, 3, 1, 4}, then the function should return true because the array has consecutive numbers from 1 to 5. If arr

  • Most efficient way to count a range of numbers without bias? 2011-10-31

    I want to print a range of numbers to STDOUT. But rather than count from 0, 1 ,2 ... N-1, N, I want to iterate using breadth first search. I want to do this with the fewest/least intensive instructions possible (i.e. without branching). For example,

  • What is the best algorithm to see if my number is in an array of ranges? 2011-12-10

    I have a 2 dimensional arrays in php containing the Ranges. for example: From.........To --------------- 125..........3957 4000.........5500 5217628......52198281 52272128.....52273151 523030528....523229183 and so on and it is a very long list. now

  • Random number between x and y excluding a range of numbers inbetween 2012-04-30

    I am implementing a system at the moment where it needs to allocate a number in a certain range to a person, but not use any number that has been used before. Keep in mind, both the number range and exclusion list are both going to be quite large. In

Copyright (C), All Rights Reserved.

processed in 0.121 (s). 11 q(s)