🎉 Initial commit

This commit is contained in:
Patrick Müller 2021-12-27 23:39:05 +01:00
commit 19f600d29e
Signed by: Paddy
GPG Key ID: 37ABC11275CAABCE
11 changed files with 780 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.idea/
*.iml
libs
out
.DS_STORE

View File

@ -0,0 +1,17 @@
package Collections
/**
* https://www.tutorialspoint.com/kotlin/kotlin_collections.htm
*/
class Collections {
fun main1() {
val numbers = listOf("one", "two", "three", "four")
println(numbers)
}
fun main2() {
val numbers = mutableListOf("one", "two", "three", "four")
numbers.add("five")
println(numbers)
}
}

View File

@ -0,0 +1,164 @@
package Collections
/**
* https://www.tutorialspoint.com/kotlin/kotlin_lists.htm
*/
class Lists {
fun main1() {
val theList = listOf("one", "two", "three", "four")
println(theList)
val theMutableList = mutableListOf("one", "two", "three", "four")
println(theMutableList)
}
fun main2() {
val theList = listOf("one", "two", "three", "four")
println(theList.toString())
}
fun main3() {
val theList = listOf("one", "two", "three", "four")
val itr = theList.listIterator()
while (itr.hasNext()) {
println(itr.next())
}
}
fun main4() {
val theList = listOf("one", "two", "three", "four")
for (i in theList.indices) {
println(theList[i])
}
}
fun main5() {
val theList = listOf("one", "two", "three", "four")
theList.forEach {
println(it)
}
}
fun main6() {
val theList = listOf("one", "two", null, "four", "five")
println("Size of the list " + theList.size)
}
fun main7() {
val theList = listOf("one", "two", "three", "four")
if ("two" in theList) {
println(true)
} else {
println(false)
}
}
fun main8() {
val theList = listOf("one", "two", "three", "four")
if (theList.contains("two")) {
println(true)
} else {
println(false)
}
}
fun main9() {
val theList = listOf("one", "two", "three", "four")
if (theList.isEmpty()) {
println(true)
} else {
println(false)
}
}
fun main10() {
val theList = listOf("one", "two", "three", "four")
println("Index of 'two' : " + theList.indexOf("two"))
}
fun main11() {
val theList = listOf("one", "two", "three", "four")
println("Element at 3rd position " + theList.get(2))
}
fun main12() {
val firstList = listOf("one", "two", "three")
val secondList = listOf("four", "five", "six")
val resultList = firstList + secondList
println(resultList)
}
fun main13() {
val firstList = listOf("one", "two", "three")
val secondList = listOf("one", "five", "six")
val resultList = firstList - secondList
println(resultList)
}
fun main14() {
val theList = listOf("one", "two", "three", "four", "five")
val resultList = theList.slice(2..4)
println(resultList)
}
fun main15() {
val theList = listOf("one", "two", null, "four", "five")
val resultList = theList.filterNotNull()
println(resultList)
}
fun main16() {
val theList = listOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultList = theList.filter { it > 30 }
println(resultList)
}
fun main17() {
val theList = listOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultList = theList.drop(3)
println(resultList)
}
fun main18() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.groupBy { it % 3 }
println(resultList)
}
fun main19() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.map { it / 3 }
println(resultList)
}
fun main20() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.chunked(3)
println(resultList)
}
fun main21() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.windowed(3)
println(resultList)
}
fun main22() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.windowed(3, 3)
println(resultList)
}
fun main23() {
val theList = mutableSetOf(
10,
20,
30
)
theList.add(40)
theList.add(50)
println(theList)
theList.remove(10)
theList.remove(30)
println(theList)
}
}

View File

@ -0,0 +1,186 @@
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)
}
fun main2() {
val theMap = HashMap<String, Int>()
theMap["one"] = 1
theMap["two"] = 2
theMap["three"] = 3
theMap["four"] = 4
println(theMap)
}
fun main3() {
val theMap = mapOf(
Pair("one", 1),
Pair("two", 2),
Pair("three", 3)
)
println(theMap)
}
fun main4() {
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)
}
fun main5() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
println(theMap.toString())
}
fun main6() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
val itr = theMap.keys.iterator()
while (itr.hasNext()) {
val key = itr.next()
val value = theMap[key]
println("${key}=$value")
}
}
fun main7() {
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() {
val theMap = mapOf(
"one" to 1,
"two" to 2,
"three" to 3,
"four" to 4
)
theMap.forEach { k, v ->
println("Key = $k, Value = $v")
}
}
fun main9() {
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())
}
fun main10() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
if (theMap.containsKey("two")) {
println(true)
} else {
println(false)
}
if (theMap.containsValue(2)) {
println(true)
} else {
println(false)
}
}
fun main11() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
if (theMap.isEmpty()) {
println(true)
} else {
println(false)
}
}
fun main12() {
val theMap = mapOf(
"one" to 1,
"two" to 2,
"three" to 3,
"four" to 4
)
println("The value for key two " + theMap.get("two"))
println("The value for key two " + theMap["two"])
}
fun main13() {
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)
}
fun main14() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3)
val theKeyList = listOf("one", "four")
val resultMap = theMap - theKeyList
println(resultMap)
}
fun main15() {
val theMap = mutableMapOf(
"one" to 1,
"two" to 2,
"three" to 3,
"four" to 4
)
theMap.remove("two")
println(theMap)
theMap -= listOf("three")
println(theMap)
}
fun main16() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
var resultMap = theMap.toSortedMap()
println(resultMap)
}
fun main17() {
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
var resultMap = theMap.filterValues { it > 2 }
println(resultMap)
resultMap = theMap.filterKeys { it == "two" }
println(resultMap)
resultMap = theMap.filter { it.key == "two" || it.value == 4 }
println(resultMap)
}
fun main18() {
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)
}
fun main19() {
val theMap = mutableMapOf(
"one" to 1,
"two" to 2,
"three" to 3,
"four" to 4
)
theMap.put("four", 4)
println(theMap)
theMap["five"] = 5
println(theMap)
theMap.remove("two")
println(theMap)
}
}

View File

@ -0,0 +1,166 @@
package Collections
/**
* https://www.tutorialspoint.com/kotlin/kotlin_sets.htm
*/
class Sets {
fun main1() {
val theSet = setOf("one", "two", "three", "four")
println(theSet)
val theMutableSet = mutableSetOf("one", "two", "three", "four")
println(theMutableSet)
}
fun main2() {
val theSet = setOf("one", "two", "three", "four")
println(theSet.toString())
}
fun main3() {
val theSet = setOf("one", "two", "three", "four")
val itr = theSet.asIterable().iterator()
while (itr.hasNext()) {
println(itr.next())
}
}
fun main4() {
val theSet = setOf("one", "two", "three", "four")
for (i in theSet.indices) {
println(theSet.elementAt(i))
}
}
fun main5() {
val theSet = setOf("one", "two", "three", "four")
theSet.forEach {
println(it)
}
}
fun main6() {
val theSet = setOf("one", "two", null, "four", "five")
println("Size of the Set " + theSet.size)
}
fun main7() {
val theSet = setOf("one", "two", "three", "four")
if ("two" in theSet) {
println(true)
} else {
println(false)
}
}
fun main8() {
val theSet = setOf("one", "two", "three", "four")
if (theSet.contains("two")) {
println(true)
} else {
println(false)
}
}
fun main9() {
val theSet = setOf("one", "two", "three", "four")
if (theSet.isEmpty()) {
println(true)
} else {
println(false)
}
}
fun main10() {
val theSet = setOf("one", "two", "three", "four")
println("Index of 'two' - " + theSet.indexOf("two"))
}
fun main11() {
val theSet = setOf("one", "two", "three", "four")
println("Element at 3rd position " + theSet.elementAt(2))
}
fun main12() {
val firstSet = setOf("one", "two", "three")
val secondSet = setOf("one", "four", "five", "six")
val resultSet = firstSet + secondSet
println(resultSet)
}
fun main13() {
val firstSet = setOf("one", "two", "three")
val secondSet = setOf("one", "five", "six")
val resultSet = firstSet - secondSet
println(resultSet)
}
fun main14() {
val theSet = setOf("one", "two", null, "four", "five")
val resultSet = theSet.filterNotNull()
println(resultSet)
}
fun main15() {
val theSet = setOf(10, 20, 30, 31, 40, 50, -1, 0)
var resultSet = theSet.sorted()
println(resultSet)
resultSet = theSet.sortedDescending()
println(resultSet)
}
fun main16() {
val theSet = setOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultSet = theSet.filter { it > 30 }
println(resultSet)
}
fun main17() {
val theSet = setOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultSet = theSet.drop(3)
println(resultSet)
}
fun main18() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.groupBy { it % 3 }
println(resultSet)
}
fun main19() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.map { it / 3 }
println(resultSet)
}
fun main20() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.chunked(3)
println(resultSet)
}
fun main21() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.windowed(3)
println(resultSet)
}
fun main22() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.windowed(3, 3)
println(resultSet)
}
fun main23() {
val theSet = mutableSetOf(
10,
20,
30
)
theSet.add(40)
theSet.add(50)
println(theSet)
theSet.remove(10)
theSet.remove(30)
println(theSet)
}
}

View File

@ -0,0 +1,48 @@
package ControlFlow
/**
* https://www.tutorialspoint.com/kotlin/kotlin_break_continue.htm
*/
class BreakStatements {
fun main1(args: Array<String>) {
var i = 0
while (i++ < 100) {
println(i)
if (i == 3) {
break
}
}
}
fun main2(args: Array<String>) {
outerLoop@ for (i in 1..3) {
innerLoop@ for (j in 1..3) {
println("i = $i and j = $j")
if (i == 2) {
break@outerLoop
}
}
}
}
fun main3(args: Array<String>) {
var i = 0
while (i++ < 6) {
if (i == 3) {
continue
}
println(i)
}
}
fun main4(args: Array<String>) {
outerLoop@ for (i in 1..3) {
innerLoop@ for (j in 1..3) {
if (i == 2) {
continue@outerLoop
}
println("i = $i and j = $j")
}
}
}
}

View File

@ -0,0 +1,32 @@
package ControlFlow
/**
* https://www.tutorialspoint.com/kotlin/kotlin_for_loop.htm
*/
class ForStatements {
fun main1(args: Array<String>) {
for (item in 1..5) {
println(item)
}
}
fun main2(args: Array<String>) {
for (item in 5 downTo 1 step 2) {
println(item)
}
}
fun main3(args: Array<String>) {
var fruits = arrayOf("Orange", "Apple", "Mango", "Banana")
for (item in fruits) {
println(item)
}
}
fun main4(args: Array<String>) {
var fruits = arrayOf("Orange", "Apple", "Mango", "Banana")
for (index in fruits.indices) {
println(fruits[index])
}
}
}

View File

@ -0,0 +1,50 @@
package ControlFlow
/**
* https://www.tutorialspoint.com/kotlin/kotlin_if_else_expression.htm
*/
class IfStatements {
fun main1(args: Array<String>) {
val age: Int = 10
val result = if (age > 18) {
"Adult"
} else {
"Minor"
}
println(result)
}
fun main2(args: Array<String>) {
val age: Int = 10
val result = if (age > 18) "Adult" else "Minor"
println(result)
}
fun main3(args: Array<String>) {
val age: Int = 10
val result = if (age > 18) {
println("Given condition is true")
"Adult"
} else {
println("Given condition is false")
"Minor"
}
print("The value of result : ")
println(result)
}
fun main4(args: Array<String>) {
val age: Int = 20
val result = if (age > 12) {
if (age > 12 && age < 20) {
"Teen"
} else {
"Adult"
}
} else {
"Minor"
}
print("The value of result : ")
println(result)
}
}

View File

@ -0,0 +1,85 @@
package ControlFlow
/**
* https://www.tutorialspoint.com/kotlin/kotlin_when_expression.htm
*/
class WhenStatements {
fun main1(args: Array<String>) {
val day = 2
val result = when (day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4 -> "Thursday"
5 -> "Friday"
6 -> "Saturday"
7 -> "Sunday"
else -> "Invalid day."
}
println(result)
}
fun main2(args: Array<String>) {
val day = 2
when (day) {
1 -> println("Monday")
2 -> println("Tuesday")
3 -> println("Wednesday")
4 -> println("Thursday")
5 -> println("Friday")
6 -> println("Saturday")
7 -> println("Sunday")
else -> println("Invalid day.")
}
}
fun main3(args: Array<String>) {
val day = 2
when (day) {
1, 2, 3, 4, 5 -> println("Weekday")
else -> println("Weekend")
}
}
fun main4(args: Array<String>) {
val day = 2
when (day) {
in 1..5 -> println("Weekday")
else -> println("Weekend")
}
}
fun main5(args: Array<String>) {
val x = 20
val y = 10
val z = 10
when (x) {
(y + z) -> print("y + z = x = $x")
else -> print("Condition is not satisfied")
}
}
fun main6(args: Array<String>) {
val day = 2
when (day) {
1 -> {
println("First day of the week")
println("Monday")
}
2 -> {
println("Second day of the week")
println("Tuesday")
}
3 -> {
println("Third day of the week")
println("It is Wednesday, my dudes!")
}
4 -> println("Thursday")
5 -> println("Friday")
6 -> println("Saturday")
7 -> println("Sunday")
else -> println("Invalid day.")
}
}
}

View File

@ -0,0 +1,22 @@
package ControlFlow
/**
* https://www.tutorialspoint.com/kotlin/kotlin_while_loop.htm
*/
class WhileStatements {
fun main1(args: Array<String>) {
var i = 5
while (i > 0) {
println(i)
i--
}
}
fun main2(args: Array<String>) {
var i = 5
do {
println(i)
i--
} while (i > 0)
}
}

5
src/main/kotlin/Main.kt Normal file
View File

@ -0,0 +1,5 @@
import ControlFlow.IfStatements
fun main() {
val ifStuff = IfStatements()
}