Home > OS >  it is possible to fill an array from 0 to n when inizializing?
it is possible to fill an array from 0 to n when inizializing?

Time:09-22

val generatedArray = IntArray(10) { i -> i   1 }

[1,2,3,4,5,6,7,8,9,10]

I want the array to start with 0

[0,1,2,3,4,5,6,7,8,9]

CodePudding user response:

First, if you start with zero and want to go to the Nth number, your array would have to be of size N 1.

Second, this would be pretty simple -

val generatedArray = IntArray(11) { i -> i }

This would generate the requested array - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

CodePudding user response:

just use i instead of i 1

val generatedArray = IntArray(10) { i -> i }

  • Related