I am using Kotlin REPL in IntelliJ Idea
I have an ArrayList
.
val myArrayList: ArrayList<String?> = arrayListOf("pig", null, "cow", "chicken", "chicken", "duck", "turkey", "pig", null, null, "turkey", "pig")
the operation
myArrayList.subList(2, 3)
gives me the output:
res5: kotlin.collections.MutableList<kotlin.String?> = [cow]
But the operation
val myArraySubList: ArrayList<String?> = myArrayList.subList(2, 3)
gives me the error:
error: type mismatch: inferred type is MutableList<String?> but kotlin.collections.ArrayList<String?> /* = java.util.ArrayList<String?> */ was expected
val myArraySubList: ArrayList<String?> = myArrayList.subList(2, 3)
^
So, here how can I store the result of a subList
operation on an ArrayList
in another ArrayList
?
ArrayList(myArrayList.subList(2, 3))
using this constructor:
<init>(elements: Collection<E>)
But consider whether you actually need it: using ArrayList
explicitly instead of MutableList
is very rarely useful.