Liuk Liuk - 6 months ago 17
Java Question

How to create a range of int

How I can create a range (array) of

starting from two values (start and end respectively).

I have 2 variables:

int x, y;

And I want to build the range like
[x - y, x + y]


In Java 8 you can use IntStream:

int[] array = IntStream.rangeClosed(x - y, x + y).toArray();

It will include the x + y value, if you want to exclude it, use:

int[] array = IntStream.range(x - y, x + y).toArray();