Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

University of The People

CS 4405 – Mobile Applications

Written Assignment 2

Instructor: Raval Dipakkumar


Lists:

Lists are ordered collections of elements that allow duplicates.

You can access elements in a list using indices (integer positions).

Elements can repeat any number of times within a list. List in Kotlin is immutable.

Example:

Kotlin

val fruits = listOf("apple", "banana", "orange", "grape")

println("First fruit: ${fruits[0]}")

println("Last fruit: ${fruits.last()}")

for (fruit in fruits) {

println(fruit)

In this example, we create a list of fruits and demonstrate how to access elements and iterate over

the list.
Sets:

Sets are unordered collections of unique elements.

They do not allow duplicate values.

Example:

Kotlin

val uniqueNumbers = setOf(1, 2, 3, 2, 4, 1)

println("Unique numbers: $uniqueNumbers")

The set will only contain distinct values (1, 2, 3, 4).

Maps:

Maps store key-value pairs, where each key is unique.

You can think of them as dictionaries or associative arrays.

Example:

Kotlin

val fruitPrices = mapOf("apple" to 1.0, "banana" to 0.8, "orange" to 1.2)

println("Price of apple: ${fruitPrices["apple"]}")

In this example, we create a map of fruit prices and retrieve the price of an apple using its key.
References

1. Install android studio. (2020, August 25). android studio.

https://developer.android.com/studio/install

2. Murphy, M. L. (2021). Elements of kotlin.

CommonsWare. https://commonsware.com/Kotlin/Kotlin-FINAL.pdf licensed under

Creative commons 4.0

You might also like