11

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
3
  • Possible duplicate of Create ArrayList from array Commented Apr 1, 2019 at 8:49
  • @Roland Sorry my bad , he tagged it as java first , I should have saw the complete code properly . In this case its duplicate of Convert Array to List in Kotlin Commented Apr 1, 2019 at 9:11
  • @ManoharReddy no problem... just saw that edit after my comment ;-) ... well... definitely similar... but... it defines a type here beforehand and gets the wrong one there... the same problem is underneath, but 2 different starting points in my opinion... I really was looking for a question mentioning that error message, but couldn't find one... wondering why the spread operator wasn't mentioned in the linked question.... Commented Apr 1, 2019 at 9:19

5 Answers 5

20

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))
Sign up to request clarification or add additional context in comments.

1 Comment

maybe you are also interested in just list.toMutableList(). it depends whether you really need that ArrayList ;-)
3

Hope it'll help.

var categoryList : ArrayList<String>?=null
val list = arrayOf("None", "ABC")
categoryList = list.toCollection(ArrayList())

Comments

1

Another approach is use arrayListOf

 categoryList =arrayListOf("None", "ABC")

Comments

0

The spread operator and arrayListOf is another option:

var categoryList : ArrayList<String>?=null
val list = arrayOf("None", "ABC")
categoryList = arrayListOf(*list)

Comments

-1

import kotlin.collections.ArrayList

var arr:List<String>=["one","two"]<br>
val array_list:ArrayList<String>=ArrayList(arr)


2 Comments

Thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts.
Notably, given that this answer is two years old, has three existing answers, including an accepted answer, it'd be useful to help explain under what circumstances your approach is preferred, and why it's different from the existing answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.