Issue
I have an ArrayList
like this :
var amplititudes : ArrayList<Int> = ArrayList()
I want to populate this with random Ints. How can I do this?
Solution
One option is to use the array constructor as following:
var amplititudes = IntArray(10) { Random.nextInt() }.asList()
Another strategy is:
var amplititudes = (1..10).map { Random.nextInt() }
EDIT
As suggested in the comment instead of creating an instance of Random
each time it is better to initialize it once:
val ran = Random
var amplititudes = (1..10).map { ran.nextInt() }
Answered By - Md Johirul Islam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.