Liuk Liuk - 4 months ago 7
Java Question

How to create a range of int

How I can create a range (array) of

int
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]
.

Answer

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();