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
mschmidt
  • 2,740
  • 4
  • 17
  • 31
AI.
  • 934
  • 2
  • 14
  • 30
  • Possible duplicate of [Create ArrayList from array](https://stackoverflow.com/questions/157944/create-arraylist-from-array) – Manohar Apr 01 '19 at 08: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](https://stackoverflow.com/questions/46662513/convert-array-to-list-in-kotlin) – Manohar Apr 01 '19 at 09: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.... – Roland Apr 01 '19 at 09:19

5 Answers5

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))
Roland
  • 22,259
  • 4
  • 57
  • 84
  • 1
    maybe you are also interested in just [`list.toMutableList()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-mutable-list.html#tomutablelist). it depends whether you really need that `ArrayList` ;-) – Roland Apr 01 '19 at 09:10
3

Hope it'll help.

var categoryList : ArrayList<String>?=null
val list = arrayOf("None", "ABC")
categoryList = list.toCollection(ArrayList())
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Md. Arif
  • 448
  • 1
  • 5
  • 13
1

Another approach is use arrayListOf

 categoryList =arrayListOf("None", "ABC")
John Joe
  • 12,412
  • 16
  • 70
  • 135
0

The spread operator and arrayListOf is another option:

var categoryList : ArrayList<String>?=null
val list = arrayOf("None", "ABC")
categoryList = arrayListOf(*list)
ggorlen
  • 44,755
  • 7
  • 76
  • 106
-1

import kotlin.collections.ArrayList

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


milon27
  • 11
  • 2
  • 1
    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. – Jeremy Caney Aug 02 '21 at 02:27
  • 1
    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. – Jeremy Caney Aug 02 '21 at 02:29