https://kotlinlang.org logo
#announcements
Title
# announcements
r

Roberto Messina

09/12/2019, 10:55 PM
Right?
s

Steve

09/12/2019, 10:57 PM
you don’t need the index
r

Ruckus

09/12/2019, 10:57 PM
You don't need
var i: Int
Copy code
fun loadArray(n: Int, v: Array<Int>) {
    repeat(n) {
        v[i] = random()
    }
}
s

Steve

09/12/2019, 10:58 PM
for (number in array) { number = random() }
😱 1
r

Ruckus

09/12/2019, 10:58 PM
@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

Steve

09/12/2019, 11:01 PM
that’s more Kotliny
r

Roberto Messina

09/12/2019, 11:01 PM
right?
r

Ruckus

09/12/2019, 11:03 PM
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

Roberto Messina

09/12/2019, 11:05 PM
i wanna do that but in a function
r

Ruckus

09/12/2019, 11:05 PM
Do what?
s

Steve

09/12/2019, 11:05 PM
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

Roberto Messina

09/12/2019, 11:07 PM
wait... i send my code
sorry if it's write in italian
s

Steve

09/12/2019, 11:13 PM
yeah, take the first method that would take an array, and just have it return one
r

Roberto Messina

09/12/2019, 11:58 PM
Thank you for your help, now my code work 😍
e

Evan R.

09/16/2019, 4:32 PM
Copy code
fun loadArray(v: Array<Int>) {
    for ((index, value) in v.withIndex()) {
        v[index] = random()
    }
}