Convert Mutable List to Array in Kotlin without bracket and comma

I am learning about the Array data structure in kotlin. The task is given an input array and i need to print in reverse that array and without bracket and comma.
The solution is i have to use mutable list to populate the input array, and print the result and replace the bracket and comma with space character.

Here is an example:
Given input in console. The first line is the size(N) of our array(A). The second line contains N space-separated integers describing array A’s elements.


41 4 3 2


2 3 4 1

In kotlin language we create code:

fun main(args: Array<String>){    val arraySize = readLine()!!.toInt()    val n = readLine()!!.split(" ").map{ it.toInt() }    var myList = MutableList<Int> = arrayListOf()    for (i in arraySize-1 downTo 0){        myList.add(n[i])    }    println(Arrays.toString(myList.toTypedArray()).replace("[","").replace("]","").replace(",","").trim()}

why i didnot use toArray extension method instead toTypedArray()? toArray will return Object[] that’s not what we want. So i use toTypedArray to convert it to an array.


  • kotlin array from mutable list
  • kotlin convert mutableListOf on arrayof
See also  Making Flutter Firebase Chat App