โ ๋ณ์์ ํจ์ ์ ์ธ
- ๋ณ์ ํ์
๋ณ์๋ช
: ์๋ฃํ= ๊ฐ
- var : ๋ณ๊ฒฝ ๊ฐ๋ฅ
- val : ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ, ์ฝ๊ธฐ ์ ์ฉ
- fun ํจ์๋ช (๋งค๊ฐ๋ณ์๋ช : ๋งค๊ฐ๋ณ์ํ์ ): ๋ฐํํ์ {๋์}
var name: String = "์ด๋ฆ"
val age: Int = 25
val x = 5
val y = 2
fun sayHello(name:String): String{
return "Hello, $name"
}
//๊ธฐ๋ณธ ์ถ๋ ฅ
println("Welcome to $name")
//ํํ์ ์ฒ๋ฆฌ ๊ฐ๋ฅ
println("ํฉ์ ${x + y}์
๋๋ค.")
โ ์กฐ๊ฑด๋ฌธ if ์ when
fun checkMax( a: Int, b:Int){
var max : Int
if (a>b){
max = a
}else{
max = b
}
println("max : $max")
}
//์ฝํ๋ฆฐ์ if๋ฌธ๋ ํํ์, ๊ฐ์ ๋ฐํํ ์ ์๋ค
fun checkMax(a: Int, b:Int){
val max = if (a>b) a else b
println("max : $max")
}
//when์ switch์ ๋น์ทํ ์ญํ ์ ์ํ
//val res = when (๊ฐ){์กฐ๊ฑด1 -> ๋ฐํ๊ฐ1 ์กฐ๊ฑด2 -> ๋ฐํ๊ฐ2 else -> ๊ธฐ๋ณธ๋ฐํ๊ฐ}
fun getLabel(score:Int){
val label = when(score){
100 -> "perfect"
in 90..99 -> "Excellent"
in 80..89 -> "Good"
in 0..79 -> "Needs Imporovement"
else -> "invlaid"
}
println("when์ ์ฌ์ฉํ ์ฑ์ ๊ตฌ๋ถ : $score ์ $label ์
๋๋ค")
}
//์กฐ๊ฑด ์๋ when
fun notCondWhen(x:Int){
val mess = when{
x>0 -> "์์"
x<0 -> "์์"
else -> "0"
}
println(println("์กฐ๊ฑด ์๋ when : $mess"))
}
// is๋ฅผ ์ฌ์ฉํ ๋ถ๊ธฐ ๊ฐ๋ฅ
fun resWhenIs(x:Any){
val resWhenIs = when(x){
is String -> "๋ฌธ์์ด , $x"
is Int -> "์ ์ , $x"
else -> "์ ์ ์์"
}
println("is๋ฅผ ์ฌ์ฉํ ํ์
๋ณ ๋ถ๊ธฐ : $resWhenIs")
}
//ํจ์ ๋ด๋ถ return ๊ฐ์ผ๋ก ์ฌ์ฉ
fun getColorCode(color: String): String = when (color) {
"red" -> "#FF0000"
"green" -> "#00FF00"
"blue" -> "#0000FF"
else -> "#000000"
}
โ ๋ฐ๋ณต๋ฌธ for ๊ณผ while
๋ฒ์ ํํ | ||
for(i in 1..5) | 1..5 | 1 2 3 4 5 |
for(i in 0 until 5) | until 5 | 0 1 2 3 4 |
for(i in 5 downTo 1) | downTo 1 | 5 4 3 2 1 |
for (i in 0..5 step 2) | step 2 | 0 2 4 |
//๋ฆฌ์คํธ ๋ฐ๋ณต
val items = listOf("a", "b", "c")
for(i in items){
println(i)
}
//์ธ๋ฑ์ค์ ๊ฐ ๋์ ์ ๊ทผ
for ((index, item) in items.withIndex()) {
println("[$index] $item")
}
//๋๋ค์
items.forEach {item -> println("Item: $item")}
//ํ๋จ ํ ์ถ๋ ฅ - ์๋ฌด๊ฒ๋ ์ถ๋ ฅ๋์ง ์์
fun countDown() {
var i = 5
while (i > 5) {
println("i = $i")
i--
}
}
//์คํ ํ ํ๋จ = i=5 ์ถ๋ ฅ ํ ์ค์ง
fun countDownDo() {
var i = 5
do {
println("i = $i")
i--
} while (i > 5)
}
โ ์์ ์ฐ์ฐ์
- ?๋ฅผ ์ฌ์ฉํ nullable ํ์ ์ฒ๋ฆฌ
fun printLeng(str:String?){
println(str?.length) // null ์ผ ๊ฒฝ์ฐ null, ์๋๋ฉด ๊ธธ์ด ์ถ๋ ฅ
// ?: ์๋น์ค ์ฐ์ฐ์
// null ์ผ ๊ฒฝ์ฐ ์ธํ
ํ ๊ธฐ๋ณธ ๊ฐ ์ ๊ณต
val ifNull = str?.length ?: 0
println("์๋น์ค ์ฐ์ฐ์ฒ๋ฆฌ ์๋ฃ ํ ๊ฐ : $ifNull") // null ์ผ ๊ฒฝ์ฐ 0 ์ถ๋ ฅ
// !!์ ์ฌ์ฉํ ๊ฐ์ ์ธ๋ํ
//nullPointerEx ๋ฐ์
// str!!.length
// ?.let์ ์ฌ์ฉํ ์์ ํ ์ฝ๋ ์คํ
//nul ์ฒดํฌ ํ ์ฝ๋ ๋ธ๋ญ ์คํ
str?.let{println("๊ธธ์ด : {it.length}")} //null ์๋๋ฉด ์คํ
}
โ Collection Types - ๊ฐ์ ํ์ ์ ๋ชจ์ List, Set, Map
- List - ์์๊ฐ ์๋ ์ปฌ๋ ์ , ์ค๋ณต๋ ๊ฐ ํ์ฉ, ์ฝ๊ธฐ ์ ์ฉ
fun printFruits(){
val fruits = listOf("Apple","Banana","Cherry","Apple") //๋ถ๋ณ ๋ฆฌ์คํธ
println(fruits) //[Apple, Banana, Cherry, Apple]
println(fruits[1])
for(f in fruits){
println(f)
}
}
fun modifyFruits(){
val mutable = mutableListOf("Apple","Banana","Cherry") //์์ ๊ฐ๋ฅํ ๋ฆฌ์คํธ
mutable.add("Grape")
mutable[0] = "Orange"
println(mutable) //[Orange, Banana, Cherry, Grape]
}
- set - ์์ ์๋ ์ปฌ๋ ์ , ์ค๋ณต ํ์ฉ ์ํจ
fun setFruits(){
val fruits = setOf("Apple","Banana","Cherry","Apple") //์ค๋ณต ๋นํ์ฉ
println(fruits) // [Apple, Banana, Cherry]
for(f in fruits){
println(f)
}
}
fun modifySetFruits(){
val mutable = mutableSetOf("Apple","Banana","Cherry") //์ค๋ณต ์๋ ์ ๊ฑฐ ๋ฐ ์
์์ ๊ฐ๋ฅ
mutable.add("Grape")
mutable.add("Apple") //์ค๋ณต ๋ฐ์ดํฐ ์ถ๊ฐ ์๋จ
println(mutable) //[Orange, Banana, Cherry, Grape]
}
- Map<key,value> : ๊ณ ์ ํค๋ฅผ ํตํด ๊ฐ์ ํ์ํ๋ ์ปฌ๋ ์
ํ์
</key,value>
- Map<String,(Int, Int)->Int> : String ํ์ ์ ํค๋ฅผ ํตํด ์ ์ํ ๋ณ์ ๋๊ฐ๋ฅผ ๋ฐ์ ์ ์ํ์ ๊ฐ์ ๋ฐํํ ๊ฐ์ ์ฐพ๋ ๋ณ์
- mapOf : Map ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํจ์
- ํค ์์ฑ ์กฐ๊ฑด
- ๊ธฐ๋ณธ ํ์ ์ ๋ถ ๊ฐ๋ฅ ex) String, Int, Boolean ...
- ๊ฐ์ฒด ํ์
์ผ ์ ์ฐ equals() ํน์ hashCode()๊ฐ ์ ์ ๋์ด ์์ด์ผ ํ๋ค
- data class ์ฌ์ฉ ์ ์๋์ผ๋ก equals, hashCode ์์ฑ
- null ์ฌ์ฉ ๋ถ๊ฐ
// Map<key,value> = mapOf(
// ์ค์ ๊ตฌํ ๋ถ
// "๊ณ ์ ํค" to ํ ๋น ๊ฐ
// )
fun main() {
val fruits = mapOf("apple" to 3, "banana" to 2, "cherry" to 5)
println(fruits) // {apple=3, banana=2, cherry=5}
println(fruits["banana"]) // ์ถ๋ ฅ: 2
}
fun main() {
val mutableFruits = mutableMapOf("apple" to 3, "banana" to 2) //์์ ๊ฐ๋ฅ
mutableFruits["cherry"] = 5 // ์๋ก์ด ํค-๊ฐ ์ ์ถ๊ฐ
mutableFruits["apple"] = 4 // ๊ธฐ์กด ๊ฐ ์์
println(mutableFruits) //{apple=4, banana=2, cherry=5}
}
fun main() {
val add = operate(4,2){x,y -> x+y}
val sub = operate(4,2){x,y -> x-y}
val mul = operate(4,2){x,y -> x*y}
println(add)
println(sub)
println(mul)
//Map ์ฌ์ฉ
val op = operatorMap["add"] ?: { _, _ -> 0 }
// {_,_->0} : ๊ธฐ๋ณธ ๊ฐ์ผ๋ก 0์ ๋ฐํํ๋ ๋๋ค์
println(op(10, 3)) // 13
}
fun operate(a: Int, b:Int, op:(Int, Int)->Int):Int{
return op(a,b)
}
//๋์์ ํจ์ ์ธ๋ถ๋ก ์์ํด์ ์ ์ฐํ ์ฝ๋ ์์ฑ ๊ฐ๋ฅ
val operatorMap:Map<String, (Int, Int)->Int> = mapOf(
"add" to {x,y -> x+y},
"sub" to {x,y -> x-y},
"mul" to {x,y -> x*y}
)
โ ๋๋ค ํจ์ Lambda
์ด๋ฆ ์๋ ํจ์๋ก ๋ณ์์ ํ ๋นํ๊ฑฐ๋ ๋ค๋ฅธ ํจ์์ ์ธ์๋ก ์ ๋ฌ
fun main() {
val sum = { x: Int, y: Int -> x + y } // ๋ ๊ฐ์ ๋ํ๋ ๋๋ค ํจ์
println(sum(3, 5)) // 8
val greet = { name: String -> "Hello, $name!" }
println(greet("Kotlin")) // Hello, Kotlin!
var result = operate(5, 3) { a, b -> a + b }
println(result) // 8
result = operate(5, 3) { a, b -> a - b }
println(result) // 2
}
//๊ณ ์ฐจ ํจ์ - ๋๋คํจ์๊ฐ ๋งค๊ฐ๋ณ์์ธ ๊ฒฝ์ฐ
fun operate(x: Int, y: Int, op: (Int, Int) -> Int): Int {
return op(x, y) // ๋๋ค ํจ์๋ฅผ ์คํํ์ฌ ๊ฒฐ๊ณผ ๋ฐํ
}