Kotlin

Kotlin List

A list in Kotlin is simply a data structure that can hold data belonging to the same or different data types. 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. Moreover, the lists in Kotlin also have certain built-in functions associated with them. Therefore in this article, we will try to explore this data structure of the Kotlin programming language in depth.

How to Use the Lists in Kotlin?

For 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

In this illustration, we want to create and print a numbered list in Kotlin. For doing that, we have used the Kotlin script shown in the image below:

In this Kotlin program, we have defined our list named “numbers” within the “main()” function. Then, we have used the “listOf” function for assigning the elements to this list. After that, we have utilized a “for” loop that iterates over all the elements of our declared list. Then, within this loop, we have just printed all these elements on the terminal.

We can compile a Kotlin script with the following command:

$ kotlinc list.kt

Afterward, we can execute a Kotlin script with the command shown below:

$ kotlin ListKt

The numbers of our Kotlin list are shown in the following image:

Example # 2: Accessing the Elements of a List with “index” Notation in Kotlin

For iterating over a Kotlin list, we can also use another notation, unlike the one that we have used in our first example. You can go through the Kotlin program shown in the image below to learn this:

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. This proves that a Kotlin list can contain elements belonging to different data types at the same time. After that, we have used a “println” statement for printing a message on the terminal. Then, we have implemented a “for” loop inside which we have used the “index” notation for iterating over the elements of our list. The indexing of a Kotlin list starts from “0” and goes all the way up to the size of the list minus one. Then, we have printed the elements of this list within this “for” loop.

The elements belonging to different data types of our Kotlin list are shown in the following image:

Example # 3: Creating and Printing a Kotlin List Having Random Elements

In this Kotlin script, we will be creating a list having elements belonging to different data types. However, this time, we will be specifically mentioning this type. If you know the data type of the elements of a list in advance, then you can mention it at the time of declaration so that the said list can only hold that particular type of element. On the other hand, if you wish to create a list having elements belonging to varied data types, then you can also use the “Any” data type of the Kotlin programming language at the time of declaration. This is shown in the Kotlin script given below:

In this program, we have used the same random list that we have created in the Kotlin script implemented in the second example. However, this time, we have explicitly mentioned that the data type of our list is “Any”. After that, we have just used a “for” loop to print the elements of this list on the terminal.

The elements of our random list are shown in the following image:

Example # 4: Using Some of the Built-in Functions of the Kotlin Lists

This will be a generic example in which we will be using some of the built-in functions allied with the lists in Kotlin. Again, we will be using the same list that we have created in the example above. The exact Kotlin script is shown in the image below:

After creating a list with random elements in this example, we first wanted to print the element at the third index of our list. For that, we have used the “get(3)” function since the list indexing starts from “0”. Then, we wanted to print the index number of the element “Mathematics” for which we have used the “indexOf(Mathematics)” function. After that, we wanted to print the size of our list for which we have used the “size” function. Then, we wanted to check whether the element “History” exists within our list or not because of which we have used the “contains(“History”)” function. We also wanted to check whether our list was empty or not for which we have used the “isEmpty()” function. Then, we wanted to print our list while dropping its first element. Note, that the “drop” function of Kotlin considers the list indexing starting from “1”. Therefore, for dropping the first element of the list, we have used the “drop(1)” function. Finally, we want to print our list while dropping its last three elements for which we have used the “dropLast(3)” function of the Kotlin programming language.

The output of all the built-in functions of the Kotlin list that we have used in the example above is shown in the following image:

Conclusion

In this article, we wanted to elucidate to you the working of the lists in Kotlin. After giving you a brief introduction to the Kotlin lists, we moved straight on to the explanation of our examples in which we created and printed multiple types of lists. We also shared with you the different methods of iterating over these lists. Moreover, we even tried to explore some of the built-in functions of the Kotlin lists. By taking guidance from this article, you can easily work with the lists in Kotlin in Ubuntu 20.04.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.