![]() It’s Kotlin’s spread operator - the operator that unpacks an array into the list of values from the array. When to use the spread operator in Kotlin? To convert individual elements to an array you can use arrayOf (…), but you don’t need that in this case. Within the vararg -function the vararg parameter will be an array anyway. How to convert an array to a Vararg in Kotlin?Ī vararg expects individual elements, and *array is the Kotlin way to say “treat this array as individual elements for that purpose”. listOf(1, 2, 3, 4, 5, 6) returns a read-only list of integers from 1 through 6. How do I use a list on Kotlin?Ĭreate a new List using the Kotlin standard library function listOf(), and pass in the elements of the list as arguments separated by commas. We can pass n number of parameters to a vararg variable of the defined datatype or even of a generic type. Kotlin provides us to achieve the same by defining a parameter of a function as vararg. asList(1, 2, 3), or, if we already have an array and want to pass its contents to the function, we use the spread operator (prefix the array with *): From the Kotlin Reference… When we call a vararg-function, we can pass arguments one-by-one, e.g. We can also iterate over the Array list using Kotlin For loop.The * operator is known as the Spread Operator in Kotlin. ![]() When using an ArrayList, we can use its add() function to add an item to this array list. You can use ArrayList in place of arrayof() while creating the function. After adding the item we are converting the mutable list to an array using the toTypedArray() function.We are converting the array to a mutable list using the toMutableList() function and adding the item to this mutable list using add() function.We have declared an array called names.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |