Fill arrays with ranges of numbers

For those still looking for a solution:

In Java 8 or later, this can be answered trivially using Streams without any loops or additional libraries.

int[] range = IntStream.rangeClosed(1, 10).toArray();

This will produce an array with the integers from 1 to 10.

A more general solution that produces the same result is below. This can be made to produce any sequence by modifying the unary operator.

int[] range = IntStream.iterate(1, n -> n + 1).limit(10).toArray();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)