๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Kotlin

Kotlin - Scope ํ•จ์ˆ˜ & List์™€ Set

by ๋„์บ๋ฆฌ๐Ÿฑ 2021. 6. 30.
๋ฐ˜์‘ํ˜•

์Šค์ฝ”ํ”„ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ ์–‘์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. 

 

์„œ์ ์œผ๋กœ ์–ด๋ ค์›Œ์„œ ์œ ํŠœ๋ธŒ๋ฅผ ํ†ตํ•ด ๊ฐ•์ขŒ๋ฅผ ์‹œ์ฒญํ–ˆ๋‹ค. 

↓ ์ถ”์ฒœ(์‰ฝ๊ฒŒ ์ดํ•ด ๋œ๋‹ค.)

https://www.youtube.com/watch?v=QGDWWL6qA3I

https://www.youtube.com/watch?v=B-QPB487aVA

 


fun main() {

    var price = 5000

    var book = Book("๋””๋ชจ",10000)
    book.name = "[์ดˆํŠน๊ฐ€]" + book.name
    book.discount()

    // scope ํ•จ์ˆ˜ apply๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์ฐธ์กฐ ์—ฐ์‚ฐ์ž๋ฅผ ์—†์ด ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ด์ง€๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    // apply๋Š” ์ธ์Šคํ„ด์Šค ์ž์‹ ์„ ๋‹ค์‹œ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ, ์ƒ์„ฑ๋˜์ž ๋งˆ์ž ์กฐ์ž‘๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ณ€์ˆ˜์— ๋ฐ”๋กœ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.
    // main ํ•จ์ˆ˜์™€ ๋ณ„๋„์˜ scope์—์„œ ์ธ์Šคํ„ด์Šค์˜ ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ์กฐ์ž‘ํ•˜๋ฏ€๋กœ ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•ด์ง„๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.
    var book2 = Book("๋””๋ชจ2",10000).apply {
        name = "[์ดˆํŠน๊ฐ€]" + book.name
        discount()
    }

    // run ์Šค์ฝ”ํ”„๋Š” apply ์ฒ˜๋Ÿผ ์ฐธ์กฐ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•ˆํ•˜๋„ ๋œ๋‹ค๋Š” ์ ์€ ๊ฐ™์ง€๋งŒ ์ผ๋ฐ˜ ๋žŒ๋‹ค ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์ธ์Šคํ„ด์Šค ๋Œ€์‹ 
    // ๋งˆ์ง€๋ง‰ ๊ตฌ๋ฌธ์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ˜ํ™˜ ํ•œ๋‹ค. ์ฆ‰ ์ด๋ฏธ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ  ์ดํ›„์— ์ธ์Šคํ„ด์Šค์— ํ•จ์ˆ˜๋‚˜ ์†์„ฑ์„ ์Šค์ฝ”ํ”„ ๋‚ด์—์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๋•Œ ์œ ์šฉ
    book2.run{
        println("์ƒํ’ˆ๋ช… : ${name}, ๊ฐ€๊ฒฉ: ${price}์›")
   }

    //with : run๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€์ง€๋งŒ,
    // ๋‹จ์ง€ ์ธ์Šคํ„ด์Šค ์ฐธ์กฐ ์—ฐ์‚ฐ์ž ๋Œ€์‹  ํŒจ๋Ÿฌ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š”๋‹ค๋Š” ์ฐจ์ด
    // a.run {...}
    // with(a) {...}


    // also , let : ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ๋„˜๊ธด ๊ฒƒ์ฒ˜๋Ÿผ it ์„ ํ†ตํ•ด์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
    // ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚˜๋ฉด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ apply/ also
    // ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚˜๋ฉด ์ตœ์ข…๊ฐ’์„ ๋ฐ˜ํ™˜ run / let
    book2.let{
        println("์ƒํ’ˆ๋ช… : ${it.name}, ๊ฐ€๊ฒฉ: ${it.price}์›")
    }

}

class Book(var name: String, var price: Int)
{
    fun discount()
    {
        price -= 2000
    }

}

  it this
์ธ์Šคํ„ด์Šค ๋ฐ˜ํ™˜ also apply
์ตœ์ข…๊ฐ’ ๋ฐ˜ํ™˜ let run

 

with

 

 

List

import java.io.File

fun main() {
    /**
     * List: ๊ฐ ์š”์†Œ๊ฐ’์ด ์ˆœ์ฐจ์ ์œผ๋กœ ์ €์žฅ. ์ค‘๋ณต๊ฐ’ ํ—ˆ์šฉ
     * List๋Š” ์ œ๋„ค๋ฆญ ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์—, ์–ด๋–ค ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
     * โ˜… List ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋Š” List ํƒ€์ž…์— ์˜ํ•ด ๊ฒฐ์ •๋˜๋ฉฐ, '๋ณ€๊ฒฝ ๊ฐ€๋Šฅ'์€ List์— ์ €์žฅ๋œ ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.
     * โ˜… ๋ณ€๊ฒฝ ํ•˜๋ ค๋ฉด listOf ๋Œ€์‹  mutableListOf ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.
     *
     * ์œ„์˜ ๋ฐฉ์‹์ด ์•„๋‹ˆ๋”๋ผ๋„, toList ํ˜น์€ toMutableList๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ๊ธฐ ์ „์šฉ๊ณผ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ ๋ฒ„์ „ ๊ฐ„์„ ์ƒํ˜ธ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
     *
     * foreach์™€ foreachIndexed ํ•จ์ˆ˜๋Š” ์ฝ”ํ‹€๋ฆฐ์˜ ๋‹ค๋ฅธ ํƒ€์ž…์—๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.
     *
     *
     * Set: ์ค‘๋ณต ๋ถˆ๊ฐ€
    */

    // ์ ‘๊ทผ
    var customerList = mutableListOf("eli","carrie","sky")
    // var customerList: List<String> = listOf("eli","carrie","sky") - ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ฆฌ์ŠคํŠธ์ž„!

    println(customerList)
    println(customerList.first())
    println(customerList.last())
    println(customerList[0])

    // ์•ˆ์ „ํ•œ ์ธ๋ฑ์Šค
    // getOrElse ๋‘๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ๊ด„ํ˜ธ๋กœ ๋‘˜๋Ÿฌ์‹ผ ์š”์ฒญ๋œ ์ธ๋ฑ์Šค. ๋‘๋ฒˆ์งธ ์ธ์ž๋Š” ๋žŒ๋‹ค

    println(customerList.getOrElse(4){"defualt"})

    // contains
    if(customerList.contains("carrie")) {
        println("์˜ค carrie ์•ˆ์— ์žˆ์–ด์š”~")
    }else{
        println("์˜ค ์—†๋„ค์š”~")
    }

    if(customerList.containsAll(listOf("carrie","sky"))){
        println("๋„ค ๋‘˜๋‹ค ์žˆ๋„ค์š”~")
    }else{
        println("๋‚˜๊ฐ„ ์‚ฌ๋žŒ๋„ ์žˆ์–ด์š”~")
    }


    // add remove - mutable List ์—ฌ์•ผ ๊ฐ€๋Šฅ
    customerList.add("jerry")
    customerList.add("daphne")
    customerList.remove("eli")

    // ํŠน์ •์œ„์น˜์— add
    customerList.add(0,"sharon")
    customerList.add(0,"dory")

    // ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜์—ฌ ๋ฐ”๊พธ๊ธฐ
    customerList[0] = "dorydory"

    // add all (listOf("",""))
    // clear ๋ชจ๋“  ๊ฐ’ ์‚ญ์ œ

    println(customerList)


    // ๋ฆฌ์ŠคํŠธ ๋ฐ˜๋ณต ์ฒ˜๋ฆฌ : for ๋ฌธ
    for (customer in customerList){
        println("๊ณ ๋งˆ์›Œ์š” ์ •๋ง. ${customer}๋‹˜")
    }

    // ์ž๋ฐ”  for(int i = 0; i<10; i++) {}
    // ์ฝ”ํ‹€๋ฆฐ for(i in 1..10) {}

    // ferEach๋Š” List ๊ฐ ์š”์†Œ๋ฅผ ์™ผ์ชฝ ๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•˜๋‚˜์”ฉ ๊ฐ€์ ธ์˜ด. ์ธ์ž๋กœ ์ง€์ •ํ•œ ์ต๋ช…ํ•จ์ˆ˜์•  ์ „๋‹ฌํ•˜๊ณ  ์‹คํ–‰
    customerList.forEach{
            customer ->
        println("์ด๊ฑด for each ${customer}")
    }

    // forEachIndexed ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ณ ๊ฐ ์ˆœ์„œ ๋ณด์—ฌ์ฃผ๊ธฐ
    customerList.forEachIndexed { idx, customer ->
        println("์ธ๋ฑ์Šค์™€ ์ด๋ฆ„ ๋‘˜๋‹ค ์ด๋ฆ„์€ ${customer}์ด๊ณ  ${idx+1}๋ฒˆ์งธ ๋„ค์š”~!")
    }


    // ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๋ฉ”๋‰ด ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ
    val menuList = File("out/data/menu_items.txt")
        .readText()
        .split("\n")

    menuList.forEachIndexed { idx, menu ->
        println("${idx}: ${menu}")
    }

    // ํ•ด์ฒด ์„ ์–ธ -
    // List ๋Š” ๋งจ ์•ž์˜ ๋‹ค์„ฏ ๊ฐœ ์š”์†Œ๊นŒ์ง€ ๋ณ€์ˆ˜๋กœ ํ•ด์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.
    // ํ•ด์ฒด๋ฅผ ์›ํ•˜์ง€ ์•Š๋Š” ์š”์†Œ์— ๋ฐ‘์ค„ ๊ธฐํ˜ธ(_)๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์„ ํƒ์ ์œผ๋กœ ํ•ด์ฒด ๊ฐ€๋Šฅ
}

 

Set

While

import java.io.File

/**
 * Set : ์ค‘๋ณต ๋ถˆ๊ฐ€๋Šฅ
 *
 * ์ฃผ๋กœ ์ธ๋ฑ์Šค ๊ธฐ๋ฐ˜์˜ ์–ด๋– ํ•œ ๊ฒƒ๋“ค์€ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.
 * add / add all /remove / removeAll/ clear
 *  += . -=.
 */

fun main() {
    val planets = setOf("Mercury","Venus","Earth")
    println(planets)

    // ์†๋‹˜ ์ด๋ฆ„
    var customerList = mutableListOf("eli","carrie","sky")

    // ์†๋‹˜ ์„ฑ
    val lastName = listOf("Jung", "Do", "Lee")
    val menuList = File("out/data/menu_items.txt")
        .readText()
        .split("\n")

    // ๊ณ ์œ ํ•œ ์ด๋ฆ„
    val uniqueName = mutableSetOf<String>()




    // ์š”์†Œ ์žˆ๋Š”์ง€ ์š”์†Œ ์ด๋ฆ„ ๋˜๋Š” ์ธ๋ฑ์Šค๋กœ ํ™•์ธ >> true false ๋กœ ๋ฐ˜ํ™˜ํ•จ.
    // elementAt์€ ์ธ๋ฑ์Šค ๊ธฐ๋ฐ˜์˜ List ์‚ฌ์šฉ๋ณด๋‹ค ๋Š๋ฆฌ๋‹ค. (์ธ๋ฑ์Šค ๊ธฐ๋ฐ˜์˜ ์ฒ˜๋ฆฌ๋ฅผ ์›ํ•œ๋‹ค๋ฉด Set ์ด ์•„๋‹Œ List๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•จ)
    println(planets.contains("Earth"))
    println(planets.elementAt(2))

    // ๋ณ€๊ฒฝ์€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ add(index, element) ์—†๋‹ค.
    // set์— ์ถ”๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต๋œ ์š”์†Œ๊ฐ€ ์‚ญ์ œ๋˜๋ฉด ๊ณ ์œ ํ•œ ๊ณ ๊ฐ ์ด๋ฆ„์„ ๊ฐ–๋Š” ์š”์†Œ๋“ค๋งŒ ๋‚จ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค.
    (0..9).forEach {
        val first = customerList.shuffled().first()
        val last = lastName.shuffled().first()
        val name = "${first} ${last}"
        uniqueName += name
       //  println(name)
    }
    println(uniqueName)


    /**
     * for ๋ฃจํ”„๋Š” ๊ฐ ์š”์†Œ๋“ค์— ๋Œ€ํ•ด ์—ฐ์†์ ์œผ๋กœ ์ฝ”๋“œ ์‹คํ–‰ํ•  ๋•Œ ์œ ์šฉ.
     * ๋ฐ˜๋ณต ์ฒ˜๋ฆฌ๋  ์ˆ˜ ์—†๋Š” ์ƒํƒœ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ด๋•Œ๋Š” while ๋ฃจํ”„ ์œ ์šฉ
     * while ๋ฃจํ”„ ๋กœ์ง : ์–ด๋–ค ์กฐ๊ฑด์ด true์ผ ๋™์•ˆ๋งŒ ์ด ๋ธ”๋ก์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ผ.
     *
     * break ํ‘œํ˜„์‹ - ๊ทธ ์ฆ‰์‹œ ๋ฃจํ”„๋ฅผ ๋ฒ—์–ด๋‚œ๋‹ค. ์ž์‹ ์ด ์‹คํ–‰๋˜๋Š” ๋ฃจํ”„๋งŒ ์ค‘๋‹จ.
     * */
    var orderCount = 0
    while(orderCount <= 9){
        uniqueName.shuffled().first()
        menuList.shuffled().first()
        orderCount ++
    }
}

 

์ปฌ๋ ‰์…˜ ๋ณ€ํ™˜

fun main() {
    // list → set → list ๋กœ ๋ฐ”๊ฟ”์คŒ์œผ๋กœ์จ ์ค‘๋ณต์„ ์ œ๊ฑฐ ํ›„ ์ธ๋ฑ์Šค์— ์ ‘๊ทผ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ–ˆ๋‹ค.
    val customer = listOf("ji do", "hojin jang","cherry")
        .toSet()
        .toList()

    println(customer[1])
}

 

 

๋ฐฐ์—ด ํƒ€์ž…

* ์ž๋ฐ”๋Š” ๋ฐฐ์—ด์ด ๊ธฐ๋ณธ ํƒ€์ž…์ธ๋ฐ ์ฝ”ํ‹€๋ฆฐ์€ Arrays๋ผ๋Š” ์ฐธ์กฐ ํƒ€์ž…์œผ๋กœ ๋ฐฐ์˜๋ฅด ์ง€์›ํ•œ๋‹ค.
* Arrays ํƒ€์ž…์€ ์ฝ”ํ‹€๋ฆฐ๊ณผ ์ž๋ฐ” ๊ฐ„ ์ƒํ˜ธ์šด์šฉ์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€