Arrays and lists in Kotlin: Getting started with these mutable and immutable structures

- Andrés Cruz

En español

Arrays and lists in Kotlin: Getting started with these mutable and immutable structures

Arrays or arrangements and lists are a fundamental element in any programming language that allow handling collections of values or data of the same type, whether they are among Kotlin primitives or our own objects; in Kotlin.

As a fundamental principle in Kotlin, which is the handling of mutable types and immutable types, this principle also extends to arrays and lists that we will see next.

Arrays in Kotlin

If we want to define an array in Kotlin we can do it using the arrayOf, arrayOfNULLs and emptyArray methods; each of these forms share common promises and functions as we will see a little later; finally, to create an array with the arrayOf method we have:

var array = arrayOf(1,2,3,4,5)

And with this we create an array of integers; If we want to print the array as a String or text chain, where we will separate each of the elements that make up the array by commas, we can use the following method:

array.joinToString()

And we get

1, 2, 3, 4, 5

Variations on the arrayOf, arrayOfNULLs, and arrayOfNULLs methods

If we want to declare the previous array with integers we use the intArrayOf method:

var array = intArrayOf(1,2,3,4,5)

If, on the contrary, we want them to be double:

var array = doubleArrayOf(1.1,2.2,3.3,4.4,5.5)

Of course, in all cases we can use the same primitives and methods as the one we mentioned above which is the joinToString() method:

array.joinToString() //imprime 1.1, 2.2, 3.3, 4.4, 5.5

To get the size of the array we use the size property:

array.size // imprime 5 en cualquiera de los casos anteriores

We can also use some functions to get or set values:

var array = doubleArrayOf(1.1,2.2,3.3,4.4,5.5)
println(array.get(2)) // imprime 3.3 

array.set(2, 2.5) // establecemos el valor 2.5 en el índice 2

println(array.get(2)) // imprime 2.5 

We can also obtain values as if it were an array:

println(array[2]) // imprime 2.5

Lists in Kotlin

As we mentioned at the beginning and as a fundamental principle in Kotlin, we have the handling of mutable types and immutable types which also lead to the use of lists, so by default, lists in Kotlin are immutable, which in other words it means, that they cannot mutate or what is the same that they cannot change, therefore Kotlin provides methods for queries, on the other hand, for immutable lists Kotlin provides methods to mutate the lists as we want, that is, method like edit or change values or remove them; first we will see the default lists or the immutable type lists:

Immutable lists

To create a list we have the predefined List class in Kotlin that allows creating lists of generic objects; that is, of any kind; to create a list of immutable elements we have:

val list = listOf(1,2,3,4,5)

Here the same organization that is presented with the Arrays that we saw previously is NOT extended, and that is that we cannot specify the data type of the list by indicating the data type before the name of the list as we did with the Arrays; that is, the following would be an invalid code:

val list = intListOf(1,2,3,4,5) // error: unresolved reference: intListOf

Mutable Lists

If, on the other hand, we need to customize the values of the list, that is, use methods to add, remove or change values, we can use mutable lists and in this way we have an extra range of options to be able to customize or change the list whenever we want; for this we must add "mutable" in front of the "ListOf" for example, to create a mutable list we have:

val mutableList = mutableListOf(1,2,3,4,5 )

To change a value, we can do it in two ways, just like with Arrays:

mutableList[1] = 85
println(mutableList[1]) // imprime 85

or through:

mutableList.set(1,90)
println(mutableList.get(1)) // imprime 90

In the two previous blocks of code, in addition to changing or setting another value to a list index, we saw how to obtain the value of the list, for this we used the conventional method using the brackets [] and among them the index or index of the list. position that we want to consult, or the get method that receives said index as a parameter, we can use the method that we prefer to establish how to obtain values.

Remove values from list using value and index.

We also have methods to eliminate the values, for this there is the removeAt method that allows to eliminate a value from the list indicating the index of the position that we want to eliminate and remove that allows to eliminate a value by means of the value of the position:

val mutableList = mutableListOf(10,20,30,400,50)

mutableList.removeAt(1)
// la lista queda mutableList [10, 30, 400, 50]

mutableList.remove(400)
// la lista queda mutableList [10, 30, 50]

Mutable and immutable lists

As we can see, by default Kotlin offers listOf and by adding the word "mutable" we can now handle mutable types, it is a kind of "rule" or tip that we can take into account when working with lists; With the mutable types, which if we take it to its base meaning, it is those that can mutate or change, we can make methods to obtain values by means of get, delete by means of and set values by means of set, which we cannot do with mutable lists

The lists also have methods and properties in common, such as the size property:

val mutableList = mutableListOf(1,2,3,4,5 )
mutableList.size // imprime 5 

Lists SetOf

In addition to the lists that we saw previously, Kotlin also has another structure similar to that of the lists, which are the setOf, which are collections of data that do not allow having repeated values, that is, they only allow unique values, that is, they are not repeated:

val set = setOf(1,1,2,3,4,5,5,1)

Will print:

[1, 2, 3, 4, 5]

This type of data collection does not allow modifying, deleting or adding new values, that is, they are of immutable type; to modify it we have to use the mutable types.

val setMutable = mutableSetOf(1,2,3,4,5)
Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 2d 12:31!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!