Dans ce programme, vous apprendrez à convertir une liste en tableau en utilisant toArray () et un tableau en liste en utilisant asList () dans Kotlin.
Exemple 1: Convertir une liste de tableaux en tableau
fun main(args: Array) ( // an arraylist of vowels val vowels_list: List = listOf("a", "e", "i", "o", "u") // converting arraylist to array val vowels_array: Array = vowels_list.toTypedArray() // printing elements of the array vowels_array.forEach ( System.out.print(it) ) )
Production
aeiou
Dans le programme ci - dessus, nous avons défini une liste de tableau, vowels_list
. Pour convertir la liste de tableaux en tableau, nous avons utilisé la toTypedArray()
méthode.
Enfin, les éléments du tableau sont imprimés en utilisant la forEach()
boucle.
Exemple 2: Convertir un tableau en liste de tableaux
fun main(args: Array) ( // vowels array val vowels_array: Array = arrayOf("a", "e", "i", "o", "u") // converting array to array list val vowels_list: List = vowels_array.toList() // printing elements of the array list vowels_list.forEach ( System.out.print(it) ) )
Production
aeiou
Pour convertir un tableau en une liste de tableaux, nous avons utilisé la toList()
méthode.
Voici le code Java équivalent: programme Java pour convertir une liste en tableau et vice-versa.