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

Ashutosh Panda

10/20/2019, 9:52 AM
Copy code
import kotlin.arrayOf as arrayOf1
var i=0
var number:Array<Int> = arrayOf(11,12,13,14,15)
var integer: MutableList<Int> = mutableListOf(5) {0}
for(x in 0 until 5) {
    
    integer[x]=number[x]
    
}
println(integer[2])
This is program and the error is
Copy code
-error: none of the following functions can be called with the arguments supplied:
@SinceKotlin @InlineOnly public inline fun <T> mutableListOf(): MutableList<Int> defined in kotlin.collections
public fun <T> mutableListOf(vararg elements: Int): MutableList<Int> defined in kotlin.collections
var integer: MutableList<Int> = mutableListOf(5) {0}-
a

Animesh Sahu

10/20/2019, 3:25 PM
Size of list is not fixed it is dynamic, i think
val integer = ArrayOfNulls<Int>()
would suit best for your work if you want fixed size of collection Tip: 1. Always use val wherever the object is noy rereferenced i.e. not changed to anothet instance of object. 2. Use val number: List<Int> = listOf(11,12,...) as they are part of kotlin api so their performence is better than java one. Now discussing about the error {} brackets are used as lambda/anonymous function, that has no meaning to mutableListOf() mutableListOf() is defined as creates a list with all the predefined arguments inside of () brackets mutable list cannot be of fixed size, for fixed size list you may use alternative mentioned at top.
2 Views