badtrip.blogg.se

Kotlin list example
Kotlin list example







kotlin list example

We can compile a Kotlin script with the following command: Then, within this loop, we have just printed all these elements on the terminal. After that, we have utilized a “for” loop that iterates over all the elements of our declared list. Then, we have used the “listOf” function for assigning the elements to this list. In this Kotlin program, we have defined our list named “numbers” within the “main()” function. For doing that, we have used the Kotlin script shown in the image below: In this illustration, we want to create and print a numbered list in Kotlin.

kotlin list example

How to Use the Lists in Kotlin?įor effectively using the lists in Kotlin in Ubuntu 20.04, you can go through the following four examples: Example # 1: Creating and Printing a Numbered List in Kotlin Therefore in this article, we will try to explore this data structure of the Kotlin programming language in depth. Moreover, the lists in Kotlin also have certain built-in functions associated with them. These lists can either be mutable or immutable i.e., we can have such lists whose values cannot change as well as the ones whose values can change. index 0 is returned.A list in Kotlin is simply a data structure that can hold data belonging to the same or different data types. Note that we have two January objects available but the first object i.e. feb is not in the list, so it will return null and january is the first Month object with name January and days 31.

kotlin list example

We have only one April Month object in the list.

  • Īpril is the Month object with name April and days 30.
  • įinally, we are printing the objects that we got from find.
  • Each find compares the name and days of the finding object in the predicate. We are finding three different months in this list. MonthList is a list of Month objects with five different objects. Month is a data class with three properties : String name, Integer days and Integer index. Data class Month ( val name : String, val days : Int, val index : Int ) fun main ( ) Explanation :









    Kotlin list example