37

Is there a simple way of converting a Java ArrayList to a Kotlin Array? The following code:

fun test(): Array<String> {
  val elems = ArrayList<String>()
  return elems.toArray()
}

gives the error:

main.kt:2:15: error: unresolved reference: ArrayList
  val elems = ArrayList<String>()
              ^

I'm parsing some JSON and don't know how many elements I'm going to end up with, but once I've read them I don't need to modify the data in any way so I thought I'd go with Kotlin arrays as the data type.

3
  • 2
    Please change the accepted answer, @bashor provides the simple answer to your question and that should be the accepted answer. Otherwise you confuse people who come and read your question and look for the direct response. Commented Dec 27, 2015 at 5:10
  • You should always post the error message of the compiler, not just "it doesn't compile" Commented Dec 27, 2015 at 5:16
  • @JaysonMinard Thanks. I added the compiler output. Commented Mar 26, 2016 at 15:05

4 Answers 4

108

Try to use extension function toTypedArray, like:

fun test(): Array<String> {
    val elems = arrayListOf<String>()
    return elems.toTypedArray()
}
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is correct for the question being asked. Comments in the other answer of @diesieben07 make valid points about lists vs. arrays but this answer is the correct behaviour when using arrays.
3

Convert ArrayList to Array in Kotlin

 import java.util.*

fun main(args: Array<String>) {

    val list = ArrayList<String>()
    list.add("a")
    list.add("b")

    val array = arrayOfNulls<String>(list.size)
    list.toArray(array)

    println(Arrays.toString(array))

}

Output : [a, b]

Convert Array to list in Kotlin

import java.util.*

fun main(args: Array<String>) {

    val array = arrayOf("a", "b")
    val list = Arrays.asList(*array)

    println(list)

}

Output : [a, b]

Comments

1

Why use an array? Kotlin Arrays are mutable just like Java Arrays. You should use a Kotlin List, which as opposed to MutableList, is immutable. As to why the code doesn't compile: toArray returns an Object[], if you want a String[] from a List you need to use the toArray version that takes an Array as it's argument.

9 Comments

Kotlin's List is a perfect choice for the job. It works on java's ArrayList, so it is as fast and compact as it can get. Generally you are not supposed to use arrays in kotlin unless you need a very specific low-level optimization. Many conventional methods are not available for arrays, for example the are not Iterable.
This is not an answer, you can answer the question AND provide an opinion at the same time. This only provides an opinion.
The correct method is toTypedArray as pointed out in the answer from @bashor, not toArray
Your opinion is valid and correct, arrays are very uncommon and usually are a choice only when optimizations need to be made, or another API forces them upon you. But to answer his direct question use the extension method on the ArrayList of toTypedArray to convert it to an Array.
Immutable or not isn't a very strong argument for arrays versus a read only Kotlin list. A stronger argument is for using a List because it has more functionality easily available to it, more API's accepts lists than arrays (Jetbrains did statistical analysis on array usage at one point), and there isn't any real performance benefit of an array for this use case. Best to stay with List unless optimization is needed for memory/speed and usually profiling will tell you when that step is needed.
|
-2

you can use kotlin list it is easy to use why converting java array to kotlin array both are mutable.

try this....

fun learnlist(){
 var stringList= listOf<String>("a","b","c","d")
    for (st in stringList)
        print(st)

    var intlist= listOf<Int>(1,2,3,4,5)
    for(i in intlist)
        print(i)

}

Comments

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.