Annotating all the things so you actually know what they do

This commit is contained in:
Patrick Müller 2021-12-30 13:47:45 +01:00
parent 19f600d29e
commit dd093fd933
Signed by: Paddy
GPG Key ID: 37ABC11275CAABCE
10 changed files with 430 additions and 133 deletions

View File

@ -4,12 +4,19 @@ package Collections
* https://www.tutorialspoint.com/kotlin/kotlin_collections.htm
*/
class Collections {
fun main1() {
/**
* How to instantiate an immutable list of strings
*/
fun createImmutableList() {
val numbers = listOf("one", "two", "three", "four")
// numbers.add("five") not possible
println(numbers)
}
fun main2() {
/**
* How to instantiate a mutable list of strings
*/
fun createMutableList() {
val numbers = mutableListOf("one", "two", "three", "four")
numbers.add("five")
println(numbers)

View File

@ -4,19 +4,30 @@ package Collections
* https://www.tutorialspoint.com/kotlin/kotlin_lists.htm
*/
class Lists {
fun main1() {
/**
* How to instantiate a mutable and immutable list of strings.
* Output is the same for both.
*/
fun printMutableImmutable() {
val theList = listOf("one", "two", "three", "four")
println(theList)
val theMutableList = mutableListOf("one", "two", "three", "four")
println(theMutableList)
}
fun main2() {
/**
* toString() just produces the same string as direct printing would
*/
fun listToString() {
val theList = listOf("one", "two", "three", "four")
println(theList.toString())
}
fun main3() {
/**
* How to iterate over a list with a while loop
*/
fun iterateOverListWhile() {
val theList = listOf("one", "two", "three", "four")
val itr = theList.listIterator()
while (itr.hasNext()) {
@ -24,26 +35,38 @@ class Lists {
}
}
fun main4() {
/**
* How to iterate over a list using a for loop
*/
fun iterateOverListFor() {
val theList = listOf("one", "two", "three", "four")
for (i in theList.indices) {
println(theList[i])
}
}
fun main5() {
/**
* How to iterate over a list using a forEach loop
*/
fun iterateOverListForEach() {
val theList = listOf("one", "two", "three", "four")
theList.forEach {
println(it)
}
}
fun main6() {
/**
* How to get the number of elements in a list
*/
fun getListSize() {
val theList = listOf("one", "two", null, "four", "five")
println("Size of the list " + theList.size)
}
fun main7() {
/**
* Check, if an element is present in a list
*/
fun checkElementInList() {
val theList = listOf("one", "two", "three", "four")
if ("two" in theList) {
println(true)
@ -52,7 +75,10 @@ class Lists {
}
}
fun main8() {
/**
* Another way of checking the presence of an element in a list
*/
fun checkElementInList2() {
val theList = listOf("one", "two", "three", "four")
if (theList.contains("two")) {
println(true)
@ -61,7 +87,10 @@ class Lists {
}
}
fun main9() {
/**
* How to check if a list is empty
*/
fun checkEmptyList() {
val theList = listOf("one", "two", "three", "four")
if (theList.isEmpty()) {
println(true)
@ -70,95 +99,138 @@ class Lists {
}
}
fun main10() {
/**
* How to get the index of an element in a list
*/
fun getIndexOfElement() {
val theList = listOf("one", "two", "three", "four")
println("Index of 'two' : " + theList.indexOf("two"))
println("Index of 'two' : " + theList.indexOf("two")) // 1
println("Index of 'five' : " + theList.indexOf("five")) // -1
}
fun main11() {
/**
* Get the element at the specified index of the list
*/
fun getElementAtIndex() {
val theList = listOf("one", "two", "three", "four")
println("Element at 3rd position " + theList.get(2))
}
fun main12() {
/**
* How to combine to lists
*/
fun combineTwoLists() {
val firstList = listOf("one", "two", "three")
val secondList = listOf("four", "five", "six")
val resultList = firstList + secondList
println(resultList)
}
fun main13() {
/**
* How to remove all elements of one list from another, if they are present
*/
fun removeListFromList() {
val firstList = listOf("one", "two", "three")
val secondList = listOf("one", "five", "six")
val resultList = firstList - secondList
println(resultList)
println(resultList) // [two, three]
}
fun main14() {
/**
* Slices the list, i.e. creates a sublist
*/
fun sliceList() {
val theList = listOf("one", "two", "three", "four", "five")
val resultList = theList.slice(2..4)
println(resultList)
println(resultList) // [three, four, five]
}
fun main15() {
/**
* How to filter null values out of a given list
*/
fun filterNullValues() {
val theList = listOf("one", "two", null, "four", "five")
val resultList = theList.filterNotNull()
println(resultList)
}
fun main16() {
/**
* Filters all elements from a list that do not meet a defined condition
*/
fun filterElementsBasedOnCondition() {
val theList = listOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultList = theList.filter { it > 30 }
println(resultList)
println(resultList) // [31, 40, 50]
}
fun main17() {
/**
* Drops the first n elements from the list
*/
fun dropFirstnElements() {
val theList = listOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultList = theList.drop(3)
println(resultList)
println(resultList) // [31, 40, 50, -1, 0]
}
fun main18() {
/**
* Groups the elements of a list based on the given grouping criteria
*/
fun groupElementsInList() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.groupBy { it % 3 }
println(resultList)
println(resultList) // {1=[10, 31, 40], 0=[12, 30, 9, -3, 0]}
}
fun main19() {
/**
* Maps every element of the list to another value based on the given transformation
*/
fun transformElementsInList() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.map { it / 3 }
println(resultList)
}
fun main20() {
/**
* Creates chunks of the given size
*/
fun createListChunks() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.chunked(3)
println(resultList)
println(resultList) // [[10, 12, 30], [31, 40, 9], [-3, 0]]
}
fun main21() {
/**
* Creates snapshots of the list of the given size.
*/
fun createListSnapshots() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.windowed(3)
println(resultList)
println(resultList) // [[10, 12, 30], [12, 30, 31], [30, 31, 40], [31, 40, 9], [40, 9, -3], [9, -3, 0]]
}
fun main22() {
/**
* Creates snapshots of the list of the given size, using the given step size
*/
fun createSteppedListSnapshots() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.windowed(3, 3)
println(resultList)
println(resultList) // [[10, 12, 30], [31, 40, 9]]
}
fun main23() {
val theList = mutableSetOf(
/**
* How to add and remove elements from a mutable List
*/
fun addAndRemoveElements() {
val theList = mutableListOf(
10,
20,
30
)
theList.add(40)
theList.add(50)
println(theList)
println(theList) // [10, 20, 30, 40, 50]
theList.remove(10)
theList.remove(30)
println(theList)
println(theList) // [20, 40, 50]
}
}

View File

@ -4,23 +4,42 @@ package Collections
* https://www.tutorialspoint.com/kotlin/kotlin_maps.htm
*/
class Maps {
fun main1() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
println(theMap)
val theMutableMap = mutableSetOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
println(theMutableMap)
/**
* How to instantiate mutable and immutable maps
*/
fun instantiateMaps() {
val theMap = mapOf(
"one" to 1,
"two" to 2,
"three" to 3,
"four" to 4
)
println(theMap) // {one=1, two=2, three=3, four=4}
val theMutableMap = mutableMapOf(
"one" to 1,
"two" to 2,
"three" to 3,
"four" to 4
)
println(theMutableMap) // {one=1, two=2, three=3, four=4}
}
fun main2() {
/**
* How to add new elements to a map
*/
fun addElementsToMap() {
val theMap = HashMap<String, Int>()
theMap["one"] = 1
theMap["two"] = 2
theMap["three"] = 3
theMap["four"] = 4
println(theMap)
println(theMap) // {four=4, one=1, two=2, three=3}
}
fun main3() {
/**
* Another way of instantiating a map
*/
fun instantiateMap2() {
val theMap = mapOf(
Pair("one", 1),
Pair("two", 2),
@ -29,24 +48,33 @@ class Maps {
println(theMap)
}
fun main4() {
/**
* How to return entries / keys / values from a map
*/
fun getEntriesKeysValues() {
val theMap = mapOf(
"one" to 1,
"two" to 2,
"three" to 3,
"four" to 4
)
println("Entries: " + theMap.entries)
println("Keys:" + theMap.keys)
println("Values:" + theMap.values)
println("Entries: " + theMap.entries) // [one=1, two=2, three=3, four=4]
println("Keys: " + theMap.keys) // [one, two, three, four]
println("Values: " + theMap.values) // [1, 2, 3, 4]
}
fun main5() {
/**
* Map toString method returns the same string as a print would do
*/
fun mapToString() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
println(theMap.toString())
}
fun main6() {
/**
* How to iterate over a map using an iterator
*/
fun iterateWithIterator() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
val itr = theMap.keys.iterator()
while (itr.hasNext()) {
@ -56,14 +84,20 @@ class Maps {
}
}
fun main7() {
/**
* How to iterate over keys and values using a for ... in loop
*/
fun iterateWithForLoop() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
for ((k, v) in theMap) {
println("$k = $v")
}
}
fun main8() {
/**
* How to iterate over keys and values using a forEach loop
*/
fun iterateWithForEach() {
val theMap = mapOf(
"one" to 1,
"two" to 2,
@ -75,18 +109,25 @@ class Maps {
}
}
fun main9() {
/**
* How to get the size of the map.
* Both ways return the same value
*/
fun getMapSize() {
val theMap = mapOf(
"one" to 1,
"two" to 2,
"three" to 3,
"four" to 4
)
println("Size of the Map " + theMap.size)
println("Size of the Map " + theMap.count())
println("Size of the Map: " + theMap.size)
println("Size of the Map: " + theMap.count())
}
fun main10() {
/**
* How to check if a specific key or value is present in a map
*/
fun checkIfMapContainsKeyOrValue() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
if (theMap.containsKey("two")) {
println(true)
@ -100,7 +141,10 @@ class Maps {
}
}
fun main11() {
/**
* How to check if a map is empty
*/
fun checkIfMapIsEmpty() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
if (theMap.isEmpty()) {
println(true)
@ -109,7 +153,10 @@ class Maps {
}
}
fun main12() {
/**
* How to access an element in a map by its key
*/
fun accessElementsInMap() {
val theMap = mapOf(
"one" to 1,
"two" to 2,
@ -117,24 +164,33 @@ class Maps {
"four" to 4
)
println("The value for key two " + theMap.get("two"))
println("The value for key two " + theMap["two"])
println("The value for key two " + theMap["two"]) // Preferred way
}
fun main13() {
/**
* How to combine two maps into a new map
*/
fun combineTwoMaps() {
val firstMap = mapOf("one" to 1, "two" to 2, "three" to 3)
val secondMap = mapOf("one" to 10, "four" to 4)
val resultMap = firstMap + secondMap
println(resultMap)
println(resultMap) // {one=10, two=2, three=3, four=4}
}
fun main14() {
/**
* How to remove all elements with given keys from a map
*/
fun removeListOfKeysFromMap() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3)
val theKeyList = listOf("one", "four")
val resultMap = theMap - theKeyList
println(resultMap)
println(resultMap) // {two=2, three=3}
}
fun main15() {
/**
* Another way of removing elements from a map
*/
fun removeElementsFromMap() {
val theMap = mutableMapOf(
"one" to 1,
"two" to 2,
@ -142,34 +198,48 @@ class Maps {
"four" to 4
)
theMap.remove("two")
println(theMap)
println(theMap) // {one=1, three=3, four=4}
theMap -= listOf("three")
println(theMap)
println(theMap) // {one=1, four=4}
}
fun main16() {
/**
* How to sort a map
*/
fun sortMap() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
var resultMap = theMap.toSortedMap()
println(resultMap)
val resultMap = theMap.toSortedMap()
println(resultMap) // {four=4, one=1, three=3, two=2}
}
fun main17() {
/**
* How to filter elements from a map
*/
fun filterMap() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
var resultMap = theMap.filterValues { it > 2 }
println(resultMap)
println(resultMap) // {three=3, four=4}
resultMap = theMap.filterKeys { it == "two" }
println(resultMap)
println(resultMap) // {two=2}
resultMap = theMap.filter { it.key == "two" || it.value == 4 }
println(resultMap)
println(resultMap) // {two=2, four=4}
}
fun main18() {
/**
* How to map a map to another map using a sort-of lambda expression
*/
fun mapLambdaExpression() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3)
val resultMap = theMap.map { (k, v) -> "Key is $k, Value is $v" }
println(resultMap)
val resultMap = theMap.map { (k, v) ->
"Key is $k, Value is $v"
}
println(resultMap) // [Key is one, Value is 1, Key is two, Value is 2, Key is three, Value is 3]
}
fun main19() {
/**
* How to put elements into a map and remove them
*/
fun addAndRemoveElements() {
val theMap = mutableMapOf(
"one" to 1,
"two" to 2,

View File

@ -4,19 +4,28 @@ package Collections
* https://www.tutorialspoint.com/kotlin/kotlin_sets.htm
*/
class Sets {
fun main1() {
/**
* How to instantiate mutable an immutable Sets
*/
fun instantiateSets() {
val theSet = setOf("one", "two", "three", "four")
println(theSet)
println(theSet) // [one, two, three, four]
val theMutableSet = mutableSetOf("one", "two", "three", "four")
println(theMutableSet)
println(theMutableSet) // [one, two, three, four]
}
fun main2() {
/**
* How to print a set
*/
fun printSet() {
val theSet = setOf("one", "two", "three", "four")
println(theSet.toString())
println(theSet.toString()) // [one, two, three, four]
}
fun main3() {
/**
* How to iterate over a set using an iterator
*/
fun iterateOverSetWithIterator() {
val theSet = setOf("one", "two", "three", "four")
val itr = theSet.asIterable().iterator()
while (itr.hasNext()) {
@ -24,26 +33,38 @@ class Sets {
}
}
fun main4() {
/**
* How to iterate over the set indices
*/
fun iterateOverSetIndices() {
val theSet = setOf("one", "two", "three", "four")
for (i in theSet.indices) {
println(theSet.elementAt(i))
}
}
fun main5() {
/**
* How to iterate over a set using forEach
*/
fun iterateOverSetForEach() {
val theSet = setOf("one", "two", "three", "four")
theSet.forEach {
println(it)
}
}
fun main6() {
/**
* How to get the size of a set
*/
fun getSetSize() {
val theSet = setOf("one", "two", null, "four", "five")
println("Size of the Set " + theSet.size)
}
fun main7() {
/**
* How to check if an element is present in a set
*/
fun checkElementPresentInSet() {
val theSet = setOf("one", "two", "three", "four")
if ("two" in theSet) {
println(true)
@ -52,7 +73,10 @@ class Sets {
}
}
fun main8() {
/**
* Another way of checking if an element is present in a set
*/
fun checkElementPresentInSet2() {
val theSet = setOf("one", "two", "three", "four")
if (theSet.contains("two")) {
println(true)
@ -61,7 +85,10 @@ class Sets {
}
}
fun main9() {
/**
* How to check if a set is empty
*/
fun checkIfSetIsEmpty() {
val theSet = setOf("one", "two", "three", "four")
if (theSet.isEmpty()) {
println(true)
@ -70,87 +97,134 @@ class Sets {
}
}
fun main10() {
/**
* How to get the index of an element in the set
*/
fun getIndexOfElement() {
val theSet = setOf("one", "two", "three", "four")
println("Index of 'two' - " + theSet.indexOf("two"))
}
fun main11() {
/**
* How to get the element at a given index
*/
fun getElementAtIndex() {
val theSet = setOf("one", "two", "three", "four")
println("Element at 3rd position " + theSet.elementAt(2))
}
fun main12() {
/**
* How to combine two sets
*/
fun combineTwoSets() {
val firstSet = setOf("one", "two", "three")
val secondSet = setOf("one", "four", "five", "six")
val resultSet = firstSet + secondSet
println(resultSet)
}
fun main13() {
/**
* How to remove a set of elements from another set
*/
fun removeSetFromSet() {
val firstSet = setOf("one", "two", "three")
val secondSet = setOf("one", "five", "six")
val resultSet = firstSet - secondSet
println(resultSet)
println(resultSet) // [two, three]
}
fun main14() {
/**
* How to filter null values out of a set
*/
fun filterNullValues() {
val theSet = setOf("one", "two", null, "four", "five")
val resultSet = theSet.filterNotNull()
println(resultSet)
}
fun main15() {
/**
* How to sort elements in a set.
* As sets are, per their nature, unsorted, these methods return a list instead of a set
*/
fun sortSetOfElements() {
val theSet = setOf(10, 20, 30, 31, 40, 50, -1, 0)
var resultSet = theSet.sorted()
println(resultSet)
println(resultSet) // [-1, 0, 10, 20, 30, 31, 40, 50]
resultSet = theSet.sortedDescending()
println(resultSet)
println(resultSet) // [50, 40, 31, 30, 20, 10, 0, -1]
println(resultSet::class.java.typeName)
}
fun main16() {
/**
* Filters the set for elements that do not meet certain criteria
*/
fun filterElementsInSet() {
val theSet = setOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultSet = theSet.filter { it > 30 }
println(resultSet)
println(resultSet) // [31, 40, 50]
}
fun main17() {
/**
* How to drop the first n elements
*/
fun dropFirstnElements() {
val theSet = setOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultSet = theSet.drop(3)
println(resultSet)
println(resultSet) // [31, 40, 50, -1, 0]
}
fun main18() {
/**
* How to group elements of a set based in specific criteria.
* Returns a map of elements.
*/
fun groupElementsInSet() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.groupBy { it % 3 }
println(resultSet)
}
fun main19() {
/**
* Map every element of a set based on the given mapping function.
* Returns a list of elements
*/
fun lambdaMapElements() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.map { it / 3 }
println(resultSet)
}
fun main20() {
/**
* Divides the set into chunks of the given size.
* Returns a list of lists.
*/
fun divideSetIntoChunks() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.chunked(3)
println(resultSet)
println(resultSet) // [[10, 12, 30], [31, 40, 9], [-3, 0]]
}
fun main21() {
/**
* Creates snapshots of the set of the given size.
*/
fun createSetSnapshots() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.windowed(3)
println(resultSet)
}
fun main22() {
/**
* Creates snapshots of the set of the given size, using the given step size
*/
fun createSteppedSetSnapshots() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.windowed(3, 3)
println(resultSet)
}
fun main23() {
/**
* How to add and remove elements from / to the set
*/
fun addAndRemoveElements() {
val theSet = mutableSetOf(
10,
20,
@ -158,9 +232,9 @@ class Sets {
)
theSet.add(40)
theSet.add(50)
println(theSet)
println(theSet) // [10, 20, 30, 40, 50]
theSet.remove(10)
theSet.remove(30)
println(theSet)
println(theSet) // [20, 40, 50]
}
}

View File

@ -4,7 +4,10 @@ package ControlFlow
* https://www.tutorialspoint.com/kotlin/kotlin_break_continue.htm
*/
class BreakStatements {
fun main1(args: Array<String>) {
/**
* How to use break to end a single loop
*/
fun breakSingleLoop() {
var i = 0
while (i++ < 100) {
println(i)
@ -14,7 +17,10 @@ class BreakStatements {
}
}
fun main2(args: Array<String>) {
/**
* How to use break to break a specific loop
*/
fun annotatedBreak() {
outerLoop@ for (i in 1..3) {
innerLoop@ for (j in 1..3) {
println("i = $i and j = $j")
@ -25,7 +31,10 @@ class BreakStatements {
}
}
fun main3(args: Array<String>) {
/**
* How to use continue to skip to the next iteration of a single loop
*/
fun continueSingleLoop() {
var i = 0
while (i++ < 6) {
if (i == 3) {
@ -35,7 +44,10 @@ class BreakStatements {
}
}
fun main4(args: Array<String>) {
/**
* How to use continue to skip to the next iteration of a specific loop
*/
fun annotatedContinue() {
outerLoop@ for (i in 1..3) {
innerLoop@ for (j in 1..3) {
if (i == 2) {

View File

@ -4,26 +4,42 @@ package ControlFlow
* https://www.tutorialspoint.com/kotlin/kotlin_for_loop.htm
*/
class ForStatements {
fun main1(args: Array<String>) {
/**
* Normal for loop given a range to iterate over.
* Both borders are included
*/
fun forItemInRange() {
for (item in 1..5) {
println(item)
}
// 1 2 3 4 5
}
fun main2(args: Array<String>) {
/**
* Iterates over the given range with the given step size.
* Both borders are included
*/
fun forItemInRangeSteps() {
for (item in 5 downTo 1 step 2) {
println(item)
}
// 5 3 1
}
fun main3(args: Array<String>) {
/**
* How to iterate over each item in a list
*/
fun forItemInList() {
var fruits = arrayOf("Orange", "Apple", "Mango", "Banana")
for (item in fruits) {
println(item)
}
}
fun main4(args: Array<String>) {
/**
* How to iterate over a list using the list indices
*/
fun iterateOverIndices() {
var fruits = arrayOf("Orange", "Apple", "Mango", "Banana")
for (index in fruits.indices) {
println(fruits[index])

View File

@ -4,7 +4,10 @@ package ControlFlow
* https://www.tutorialspoint.com/kotlin/kotlin_if_else_expression.htm
*/
class IfStatements {
fun main1(args: Array<String>) {
/**
* How to return a value from an if statement
*/
fun ifWithReturnValue() {
val age: Int = 10
val result = if (age > 18) {
"Adult"
@ -14,13 +17,19 @@ class IfStatements {
println(result)
}
fun main2(args: Array<String>) {
/**
* Like the tertiary operator in java
*/
fun ifWithReturnTertiary() {
val age: Int = 10
val result = if (age > 18) "Adult" else "Minor"
println(result)
}
fun main3(args: Array<String>) {
/**
* Returns a value from the if statement and also does stuff
*/
fun ifWithReturnValueAndDoesStuff() {
val age: Int = 10
val result = if (age > 18) {
println("Given condition is true")
@ -33,7 +42,10 @@ class IfStatements {
println(result)
}
fun main4(args: Array<String>) {
/**
* Nested if with return value
*/
fun nestedIfWithReturn() {
val age: Int = 20
val result = if (age > 12) {
if (age > 12 && age < 20) {

View File

@ -4,7 +4,10 @@ package ControlFlow
* https://www.tutorialspoint.com/kotlin/kotlin_when_expression.htm
*/
class WhenStatements {
fun main1(args: Array<String>) {
/**
* Just like a regular switch case expression that returns a value
*/
fun switchCaseReturn() {
val day = 2
val result = when (day) {
1 -> "Monday"
@ -19,7 +22,10 @@ class WhenStatements {
println(result)
}
fun main2(args: Array<String>) {
/**
* Switch case, but with code executed for each case
*/
fun switchCaseCodeBlocks() {
val day = 2
when (day) {
1 -> println("Monday")
@ -33,7 +39,10 @@ class WhenStatements {
}
}
fun main3(args: Array<String>) {
/**
* Switch case but multiple cases result in the same code being executed
*/
fun switchCaseMultipleCases() {
val day = 2
when (day) {
1, 2, 3, 4, 5 -> println("Weekday")
@ -41,7 +50,10 @@ class WhenStatements {
}
}
fun main4(args: Array<String>) {
/**
* Switch case but the case is with a range
*/
fun switchCaseWithRange() {
val day = 2
when (day) {
in 1..5 -> println("Weekday")
@ -49,7 +61,10 @@ class WhenStatements {
}
}
fun main5(args: Array<String>) {
/**
* Use advanced mathematic shit to distinguish the relevant case
*/
fun switchCaseWithAdvancedCaseShit() {
val x = 20
val y = 10
val z = 10
@ -59,7 +74,10 @@ class WhenStatements {
}
}
fun main6(args: Array<String>) {
/**
* Executes a whole block of code for each case
*/
fun switchCaseWithMoreCodePerBlock() {
val day = 2
when (day) {
1 -> {

View File

@ -4,7 +4,10 @@ package ControlFlow
* https://www.tutorialspoint.com/kotlin/kotlin_while_loop.htm
*/
class WhileStatements {
fun main1(args: Array<String>) {
/**
* Just a normal while loop
*/
fun nomralWhile() {
var i = 5
while (i > 0) {
println(i)
@ -12,7 +15,10 @@ class WhileStatements {
}
}
fun main2(args: Array<String>) {
/**
* Normal do-while loop
*/
fun doWhile() {
var i = 5
do {
println(i)

View File

@ -1,5 +1,15 @@
import Collections.Lists
import Collections.Maps
import Collections.Sets
import ControlFlow.ForStatements
import ControlFlow.IfStatements
fun main() {
fun main(args: Array<String>) {
val ifStuff = IfStatements()
val forStuff = ForStatements()
val listStuff = Lists()
val mapStuff = Maps()
val setStuff = Sets()
forStuff.forItemInRangeSteps()
}