This file contains hidden or 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
var mutableNames1 = names.toMutableList() | |
mutableNames1.add("Ruth") // now mutable and added "Ruth" to list |
This file contains hidden or 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
// a mutable list of a certain type e.g. String | |
val mutableListNames: MutableList<String> = mutableListOf<String>("Josh", "Kene", "Sanya") | |
mutableListNames.add("Mary") | |
mutableListNames.removeAt(1) | |
mutableListNames[0] = "Oluchi" // replaces the element in index 0 with "Oluchi" | |
// a mutable list of mixed types | |
val mutableListMixed = mutableListOf("BMW", "Toyota", 1, 6.76, 'v') |
This file contains hidden or 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
val mutableListFood: MutableList<String> = mutableListOf<String>("Rice & stew", "Jollof rice", "Eba & Egusi", "Fried rice") | |
mutableListFood.remove("Fried rice") | |
mutableListFood.removeAt(0) | |
mutableListFood.set(0, "Beans") | |
mutableListFood.add(1, "Bread & tea") | |
for (foodName in mutableListFood) { | |
println(foodName) | |
} |
This file contains hidden or 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
Beans | |
Bread & tea | |
Eba & Egusi |
This file contains hidden or 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
// creates a immutable set of mixed types | |
val mixedTypesSet = setOf(2, 4.454, "how", "far", 'c') // will compile | |
var intSet: Set<Int> = setOf(1, 3, 4) // only integers types allowed |
This file contains hidden or 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
val intsHashSet: java.util.HashSet<Int> = hashSetOf(1, 2, 6, 3) | |
intsHashSet.add(5) | |
intsHashSet.remove(1) |
This file contains hidden or 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
val intsSortedSet: java.util.TreeSet<Int> = sortedSetOf(4, 1, 7, 2) | |
intsSortedSet.add(6) | |
intsSortedSet.remove(1) | |
intsSortedSet.clear() |
This file contains hidden or 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
val intsLinkedHashSet: java.util.LinkedHashSet<Int> = linkedSetOf(5, 2, 7, 2, 5) // 5, 2, 7 | |
intsLinkedHashSet.add(4) | |
intsLinkedHashSet.remove(2) | |
intsLinkedHashSet.clear() |
This file contains hidden or 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
// creates a mutable set of int types only | |
val intsMutableSet: MutableSet<Int> = mutableSetOf(3, 5, 6, 2, 0) | |
intsMutableSet.add(8) | |
intsMutableSet.remove(3) |
This file contains hidden or 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
val callingCodesMap: Map<Int, String> = mapOf(234 to "Nigeria", 1 to "USA", 233 to "Ghana") | |
for ((key, value) in callingCodesMap) { | |
println("$key is the calling code for $value") | |
} | |
print(callingCodesMap[234]) // Nigeria |