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

In Kotlin, converting between List (or ArrayList) and Array is a common task. Lists are ordered collections that can contain duplicate elements, while arrays are also ordered collections but have a fixed size. This article will explore three different Kotlin Program to Convert List (ArrayList) to Array and Vice-Versa with detailed explanations, code implementations, and outputs for each example.

1. Converting List (ArrayList) to Array

1.1 Example 1: Using toTypedArray

Program Explanation

In this example, we use the toTypedArray function to convert a List to an Array. This function creates a typed array containing all elements of the list.

Code Implementation

Kotlin
fun main() {
    val list = listOf("Kotlin", "Java", "Python")

    // Convert list to array
    val array: Array<String> = list.toTypedArray()

    println("Original List: $list")
    println("Converted Array: ${array.joinToString()}")
}

Output

Kotlin
Original List: [Kotlin, Java, Python]
Converted Array: Kotlin, Java, Python

1.2 Example 2: Using ArrayList and toArray

Program Explanation

In this example, we convert an ArrayList to an array using the toArray method. This method can be useful when working with Java interop, as it returns a generic array.

Code Implementation

Kotlin
fun main() {
    val arrayList = arrayListOf(1, 2, 3, 4, 5)

    // Convert ArrayList to array
    val array = arrayList.toArray()

    println("Original ArrayList: $arrayList")
    println("Converted Array: ${array.joinToString()}")
}

Output

Kotlin
Original ArrayList: [1, 2, 3, 4, 5]
Converted Array: 1, 2, 3, 4, 5

1.3 Example 3: Using map and toTypedArray

Program Explanation

In this example, we first transform the list using the map function and then convert it to an array using toTypedArray. This approach is useful when you need to perform operations on the elements before converting them.

Code Implementation

Kotlin
fun main() {
    val list = listOf(1, 2, 3, 4, 5)

    // Convert list to array with transformation
    val array: Array<String> = list.map { it.toString() }.toTypedArray()

    println("Original List: $list")
    println("Converted Array: ${array.joinToString()}")
}

Output

Kotlin
Original List: [1, 2, 3, 4, 5]
Converted Array: 1, 2, 3, 4, 5

2. Converting Array to List (ArrayList)

2.1 Example 1: Using toList

Program Explanation

In this example, we convert an Array to a List using the toList function. This function returns an immutable list containing all elements of the array.

Code Implementation

Kotlin
fun main() {
    val array = arrayOf("Kotlin", "Java", "Python")

    // Convert array to list
    val list: List<String> = array.toList()

    println("Original Array: ${array.joinToString()}")
    println("Converted List: $list")
}

Output

Kotlin
Original Array: Kotlin, Java, Python
Converted List: [Kotlin, Java, Python]

2.2 Example 2: Using ArrayList Constructor

Program Explanation

In this example, we convert an Array to an ArrayList using the ArrayList constructor. This approach creates a mutable list from the elements of the array.

Code Implementation

Kotlin
fun main() {
    val array = arrayOf(1, 2, 3, 4, 5)

    // Convert array to ArrayList
    val arrayList = ArrayList(array.asList())

    println("Original Array: ${array.joinToString()}")
    println("Converted ArrayList: $arrayList")
}

Output

Kotlin
Original Array: 1, 2, 3, 4, 5
Converted ArrayList: [1, 2, 3, 4, 5]

2.3 Example 3: Using forEach

Program Explanation

In this example, we convert an Array to an ArrayList by iterating through the array and adding each element to the ArrayList. This approach is useful for more complex transformations or operations on the elements.

Code Implementation

Kotlin
fun main() {
    val array = arrayOf(1, 2, 3, 4, 5)

    // Convert array to ArrayList with iteration
    val arrayList = ArrayList<Int>()
    array.forEach { arrayList.add(it) }

    println("Original Array: ${array.joinToString()}")
    println("Converted ArrayList: $arrayList")
}

Output

Kotlin
Original Array: 1, 2, 3, 4, 5
Converted ArrayList: [1, 2, 3, 4, 5]

Conclusion

In this article, we explored six different methods to convert between lists (ArrayList) and arrays in Kotlin. The first set of examples demonstrated how to convert lists to arrays using the toTypedArray function, toArray method, and map function for transformations. The second set of examples showed how to convert arrays to lists using the toList function, ArrayList constructor, and iteration with forEach. Each method provides a different approach to achieve the conversion, allowing you to choose the one that best fits your specific requirements. By understanding these techniques, you can efficiently manage and manipulate collections in your Kotlin programs.