dan
09/05/2020, 9:41 AMval res : Array<Int> = emptyArray()
res.plus(1)
println(res.size)
Paula Muldoon
09/05/2020, 9:45 AMdan
09/05/2020, 9:46 AMPaula Muldoon
09/05/2020, 9:47 AMplus()
function seems to create a new empty array (hence why it’s not throwing an error).listOf()
, which is immutable but not fixed length so you can add more elements to it. If you really need mutable, there is mutableListOf()
dan
09/05/2020, 9:53 AMadd
in my use case.Paula Muldoon
09/05/2020, 9:59 AMmyArray[1] = 2
(as long as you obey the array type).
Lists
are immutable but you can create a new list by using plus
, which would generally be safer / preferable as you aren’t modifying the original list.
val newList = oldList.plus(1)
ImmutableLists
are less safe (something else could be modifying your list at the same time) so it’s generally preferable to use an immutable list.dan
09/05/2020, 10:05 AMRuckus
09/05/2020, 3:32 PMVal res = mutableListOf<Int>()
res += 1
println(res.size) // 1