Converting between List
(or ArrayList
) and Array
is a common task in Kotlin programming. Lists are ordered collections that can contain duplicate elements and can dynamically grow, while arrays have a fixed size but provide fast access to elements. This article will demonstrate three different Kotlin Program to Convert List (ArrayList) to Array and Vice-Versa , including explanations, code implementations, and outputs for each example.
1. Converting List (ArrayList) to Array
1.1 Example 1: Using toTypedArray
Program Explanation
The toTypedArray
function converts a List
to an Array
of the same type. This method is straightforward and commonly used for conversion.
Code Implementation
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
Original List: [Kotlin, Java, Python]
Converted Array: Kotlin, Java, Python
1.2 Example 2: Using ArrayList
and toArray
Program Explanation
The toArray
method is used to convert an ArrayList
to an array. This method is useful when working with Java interoperability.
Code Implementation
fun main() {
val arrayList = arrayListOf(1, 2, 3, 4, 5)
// Convert ArrayList to array
val array = arrayList.toArray(arrayOfNulls<Int>(arrayList.size))
println("Original ArrayList: $arrayList")
println("Converted Array: ${array.joinToString()}")
}
Output
Original ArrayList: [1, 2, 3, 4, 5]
Converted Array: 1, 2, 3, 4, 5
1.3 Example 3: Using map
and toTypedArray
Program Explanation
This example demonstrates how to use the map
function to transform elements before converting the list to an array using toTypedArray
.
Code Implementation
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
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
The toList
function converts an Array
to an immutable List
. This is a simple and direct method to perform the conversion.
Code Implementation
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
Original Array: Kotlin, Java, Python
Converted List: [Kotlin, Java, Python]
2.2 Example 2: Using ArrayList
Constructor
Program Explanation
This method uses the ArrayList
constructor to convert an Array
to a mutable ArrayList
. This is useful when you need a mutable list.
Code Implementation
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
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 iterate through the Array
and add each element to an ArrayList
. This approach allows for more complex operations during the conversion process.
Code Implementation
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
Original Array: 1, 2, 3, 4, 5
Converted ArrayList: [1, 2, 3, 4, 5]
Conclusion
In this article, we explored six different methods for converting between List
(ArrayList) and Array
in Kotlin. The first set of examples showed how to convert lists to arrays using toTypedArray
, toArray
, and map
functions. The second set of examples demonstrated converting arrays to lists using toList
, ArrayList
constructor, and forEach
loop. Each method offers a different approach, allowing you to choose the one that best suits your specific requirements. By understanding these techniques, you can effectively manage and manipulate collections in your Kotlin programs.