kotlin.collections package - site for reference.
- Allows declarations of immutable or mutable collections List<Double> // immutable List of Double objects MutableList<Double> // mutable List of Double objects Arrays // array type inferred val numArray = arrayOf(1, 2, 3, 4, 5, 6, 7) // array type specified val intArray = intArrayOf(1, 3, 5, 7, 9) joinToString() println(numArray.joinToString() ) // show contents of 'numArray' (1, 2, 3 .. etc) /** Adding symbols/chars before and after the array contents In below example, parenthesis enclosing array contents (1, 2, 3, 4..) **/ println(numArray.joinToString(prefix = "(", postfix = ")")) // specifying separator between array contents other than default commas ',' println(numArray.joinToString(prefix = "(", postfix = ")", separator = "*")) // Output: (1*2*3*4... etc) Lists, Sets, Maps // immutable list val nameList = ListOf("Jen:", "Joe", "Jim") // mutable list val mutableNameList = mutableListOf("Jack", "James", "Joey") // Set: unique contents only, so the set would contain 2 and 4 only val numSet = setOf(2, 2, 4, 4) // mutable set val mutableNumSet = mutableSetOf(2, 2, 4, 4) // immutable map val alphabet = mapOf(1 to "a", 2 to "b", 3 to "c") println(map) // Output: {1=a, 2=b, 3=c} // mutable map val alphabet = mutableMapOf(1 to "a", 2 to "b", 3 to "c", 4 to "d") println(map) // Output: {1=a, 2=b, 3=c, 4=d} Comments are closed.
|
AuthorExploring Android and mobile web design, security, and development. Archives
March 2021
Categories |