W tym programie nauczysz się konwertować listę na tablicę za pomocą toArray () i tablicę na listę używając asList () w Kotlinie.
Przykład 1: Konwertuj listę tablic na tablicę
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) ) )
Wynik
aeiou
W powyższym programie, mamy zdefiniowane listy tablicy, vowels_list
. Aby przekonwertować listę tablic na tablicę, użyliśmy toTypedArray()
metody.
Na koniec elementy tablicy są drukowane za pomocą forEach()
pętli.
Przykład 2: Konwertuj tablicę na listę tablic
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) ) )
Wynik
aeiou
Aby przekonwertować tablicę na listę tablic, użyliśmy toList()
metody.
Oto równoważny kod w Javie: program w języku Java do konwersji listy na tablicę i odwrotnie.