I want to create a function that builds an array of incremental numbers.
For example, I want to get something like:
int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8, ..., 1000000};
The function will receive two parameters: the initial number (inclusive) and the final length of the array:
public int[] buildIncrementalArray(int start, int length) { ... }
I know how to do this using a for loop:
public int[] buildIncrementalArray(int start, int length) { int[] result = new int[length]; for(int i = 0 ; i < length ; i++) { result[i] = start + i; } return result; }
Instead of using a for loop, I want to use the Java 8 Stream API. Does anyone know how to do this using the Stream API?
source share