Issue
How do I convert Array<String> to ArrayList<String> in Kotlin?
var categoryList : ArrayList<String>?=null
val list = arrayOf("None", "ABC")
categoryList = ArrayList(Arrays.asList(list))
I receive this error:
Error
Type inference failed. Expected type mismatch: inferred type is kotlin.collections.ArrayList<Array<String>!> /* = java.util.ArrayList<Array<String>!> */ but kotlin.collections.ArrayList<String>? /* = java.util.ArrayList<String>? */ was expected
Solution
You may be interested in toCollection to add the elements to any collection you pass:
categoryList = list.toCollection(ArrayList())
Note that to fix your specific problem you just need the spread operator (*). With the *-fix applied it would have worked too:
categoryList = ArrayList(Arrays.asList(*list))
Answered By - Roland
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.