Liuk Liuk - 4 years ago 161
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]

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download