lasmart.blogg.se

Kotlin list of
Kotlin list of








kotlin list of

The indexing of a Kotlin list starts from “0” and goes all the way up to the size of the list minus one.

kotlin list of

Then, we have implemented a “for” loop inside which we have used the “index” notation for iterating over the elements of our list. After that, we have used a “println” statement for printing a message on the terminal. This proves that a Kotlin list can contain elements belonging to different data types at the same time. In this Kotlin script, we have created a list named “numbers”, however, this time, we have not only assigned to it the integer elements but have also assigned some strings to it. You can go through the Kotlin program shown in the image below to learn this:

kotlin list of

The numbers of our Kotlin list are shown in the following image:Įxample # 2: Accessing the Elements of a List with “index” Notation in Kotlinįor iterating over a Kotlin list, we can also use another notation, unlike the one that we have used in our first 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.

kotlin list of

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. 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. A list in Kotlin is simply a data structure that can hold data belonging to the same or different data types.










Kotlin list of