skip to Main Content
val generatedArray = IntArray(10) { i -> i + 1 }

generatedArray:

[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]

2

Answers


  1. 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]

    Login or Signup to reply.
  2. just use i instead of i+1

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search