Kotlin Program to Convert List (ArrayList) to Array and Vice-Versa

Example 1: Convert array list to array

fun main(args: Array<String>) {

    // an arraylist of vowels
    val vowels_list: List<String> = listOf("a", "e", "i", "o", "u")
    
    // converting arraylist to array
    val vowels_array: Array<String> = vowels_list.toTypedArray()
    
    // printing elements of the array 
    vowels_array.forEach { System.out.print(it) }
}

Output

aeiou

In the above program, we have defined an array list, vowels_list. To convert the array list to an array, we have used the toTypedArray() method.

Finally, the elements of the array are printed by using the forEach() loop.


Example 2: Convert array to array list

fun main(args: Array<String>) {

    // vowels array
    val vowels_array: Array<String> = arrayOf("a", "e", "i", "o", "u")
    
    // converting array to array list
    val vowels_list: List<String> = vowels_array.toList()
    
    // printing elements of the array list
    vowels_list.forEach { System.out.print(it) }
}

Output

aeiou

To convert an array to an array list, we have used the toList() method.

Here's the equivalent Java code: Java program to convert list to array and vice-versa.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges