Last active
March 14, 2020 13:54
-
-
Save magdamiu/cd055fab88ff45d5787ba7425f67032d to your computer and use it in GitHub Desktop.
Immutable and mutable lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// immutable list and mutable list | |
val numbersList = listOf("one", "two", "three") | |
val mutableNumbersList = mutableListOf("one", "two", "three") | |
// alter the list using add function | |
mutableNumbersList.add("five") | |
listOf(1, 5, 3).sum() // => 9 | |
listOf("a", "b", "cc").sumBy { it.length } // => 4 | |
// operators | |
val numberList2 = numbersList + "four" | |
println(numberList2) | |
val numbersListNoTwo = numberList2 - "two" | |
println(numbersListNoTwo) | |
println(numbersListNoTwo::class) //class java.util.ArrayList | |
println(numbersListNoTwo.javaClass) //class java.util.ArrayList | |
// listOfNotNull | |
val notNulls = listOfNotNull(32, null, "one", null, 'd') | |
println("Size = ${notNulls.size}") | |
for (element in notNulls) { | |
println(element) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment