În acest program, veți învăța să convertiți o listă într-o matrice folosind toArray () și matrice în listă utilizând asList () în Kotlin.
Exemplul 1: convertiți lista matrice în matrice
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) ) )
Ieșire
aeiou
În programul de mai sus, am definit o listă de matrice vowels_list
,. Pentru a converti lista de matrice într-o matrice, am folosit toTypedArray()
metoda.
În cele din urmă, elementele matricei sunt tipărite utilizând forEach()
bucla.
Exemplul 2: convertiți tabloul în lista de tablouri
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) ) )
Ieșire
aeiou
Pentru a converti o matrice într-o listă de matrice, am folosit toList()
metoda.
Iată codul Java echivalent: programul Java pentru a converti lista în matrice și invers.