I would like to fill an array using consecutive integers. I have created an array that contains as much indexes as the user enters:
Scanner in = new Scanner(System.in);
int numOfValues = in.nextInt();
int [] array = new int[numOfValues];
How do i fill this array with consecutive numbers starting from 1? All help is appreciated!!!
Since Java 8
The
range
is in increments of 1. The javadoc is here.Or use
rangeClosed
You now have an empty array
So you need to iterate over each position (0 to size-1) placing the next number into the array.
One more thing. If I want to do the same with reverse:
I got the normal order again..
The simple way is: