Indexing

Lists and Maps have an option to get or set an item with the indexing operator, just as you can do with the arrays. Here’s how you would get an item from the first position of a list:

val superHeros = mutableListOf("Superman", "Batman")
val superman = superHeros[0]

And this is how you would set an item at a certain position:

superHeros[2] = "Thor"

In the case of maps, you provide the key in the square brackets and the corresponding value is returned:

val map = mutableMapOf(1 to "one", 2 to "two")
val two = map[2]

And if you want to insert something into a map, you can put the key in the square brackets and then assign a value to that key:

map[3] = "three"
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.123.160