r
Right?
s
you don’t need the index
r
You don't need
var i: Int
Copy code
fun loadArray(n: Int, v: Array<Int>) {
    repeat(n) {
        v[i] = random()
    }
}
s
for (number in array) { number = random() }
😱 1
r
@Steve That won't work...
@Roberto Messina If you want an array of random integers, you could use
Copy code
val v = IntArray(n) { random() }
1
s
that’s more Kotliny
r
right?
r
No, that function doesn't do anything. If you want to update the first n options of an existing array, use the first example I showed. My second example eliminates the need for the function if your purpose is just to create an array of random ints.
r
i wanna do that but in a function
r
Do what?
s
there’s zero reason to have a function to do that
what you’d want to do, is have loadArray take the n, and return an array, if you really want it to be a method
r
wait... i send my code
sorry if it's write in italian
s
yeah, take the first method that would take an array, and just have it return one
r
Thank you for your help, now my code work 😍
e
Copy code
fun loadArray(v: Array<Int>) {
    for ((index, value) in v.withIndex()) {
        v[index] = random()
    }
}