Self-study/Kotlin

[Kotlin] Basic2

Munyoung 2024. 1. 19. 13:02

1. ++/--

    var num = 5
    println(num++) //5
    println(++num) //7
    println(num--) //7
    println(--num) //5

 

2. toInt()/toFloat()

toInt() : change the type to integer 

toFloat(): change the type to float 

    println("10" + 6)
    println(10 + "6".toInt()) //16

     val num = 5.6757565765667
     println(num)
     println(num.toFloat())
     println(num.toInt())

3. arrayOf  : immutable(cannot be resized)

1) contentToString() 

     val species = arrayOf("sponge", "star", "snail")

     println(species) // just the random memory address is printed 
     println(species.contentToString()) //print the member of the array

 

 

      val arr1 = arrayOfNulls<String>(5)
      val arr2 = intArrayOf(5,7,9)
      val arr3 = booleanArrayOf(true, false, false)

      println(arr1.contentToString()) // [null, null, null, null, null]
      println(arr2.contentToString()) // [5, 7, 9]
      println(arr3.contentToString()) // [true, false, false]

 

2) index

      val species = arrayOf("sponge", "star", "snail", "crab")
      species[0] = "squirrel"
      println(species.size) // 4
      println(species.contentToString()) // [squirrel, star, snail, crab]
      species[3] = "monkey"
      println(species.contentToString()) 

      val str =  if("squirrel" in species) "found" else "not found"
      println(str)

      println(species.indexOf("star")) // 1
      println(species.first()) //sponge
      println(species.last()) //snail
      println(species.contains("sponge")) //true

 

3) deconstructing

      val(species1, species2, _, species4) = species //_: omit the element
      println("$species1 $species2 $species4") //squirrel star monkey

 

4. List(mutable, can resize it)

1) listOf

★ 주의!

array의 경우 println(species) 했을 경우, memory address가 나옴. 하지만 list는 element가 나옴. 

그 이유는 list는 mutable 하기 때문임. 

      val species = listOf("sponge", "star", "snail", "crab")
      val result1 = species.containsAll(listOf("snail", "squirrel")) //false
      val result2 = species.containsAll(listOf("crab", "star")) //true
      println(species) // [sponge, star, snail, crab] 
      println(result1) // false
      println(result2) //true

 

2) mutableListOf = ArrayList in java 

add(): add the element at the end of the list

addAll(): add several element at the end of the list

removeAt(index): remove the element in the index

removeFirst(): remove the first element in the list. 

shuffle(): shuffle the elements in the array

sort(): sort the element in alphabetical order 

val species = mutableListOf("sponge", "star", "snail", "crab") //same as ArrayList
      println(species) //[sponge, star, snail, crab]
      species.add("whale")
      println(species) // [sponge, star, snail, crab, whale]
      species.addAll(listOf("whale", "plankton"))
      println(species)
      species.removeAt(3)
      println(species)
      species.removeFirst()
      println(species)
      
      species.shuffle()
      println(species)
      species.sort()
      println(species) //alphabetically

 

5. for loop

      val animals = listOf("sponge", "crab", "whale", "squirrel")
      for(animal in animals){
            val toUpper = animal.uppercase()
            println(toUpper)
      }

      for(index in animals.indices.reversed()){
            println("name $index is ${animals[index]}")
      }

 

- range

for(i in 1..3) println(i)

 

- step through

for(i in 3 downTo 1) println(i)

 

- forEach

animals.forEach{
            println(it)
      }

 

6. while loop

     val name = "spongebob"

      var i = 0
      while(i <= name.length - 1){
            println(name[i])
            i++
      }

 

-reverse the array

     val species = arrayOf("sponge", "crab", "whale", "plankton")

      var i = species.size -1
      while(i >= 0){
            println(species[i])
            i--
      }

 

7. continue and break

val animals = arrayOf("sponge", "crab", "whale", "plankton", "star")
      for(animal in animals){
            if(animal.length > 6){
                  continue
            }
            println(animal)
      }

      for(i in 0..10){
            if(i>6){
                  break
            }
            println(i)
      }

8. read line and print line

println("Enter some text: ")
      val input = readln()
      println("You entered: $input")

      val sc = Scanner(System.`in`)
      println("Enter a number: ")
      val num = sc.nextInt()
      println("$num doubled is: ${num*2}")

      println("Enter a number: ")
      while(true){
            val num = readln().toIntOrNull()
            if(num != null){
                  println("$num doubled is: ${num*2}")
            }else{
                  println("Please enter a valid number")
            }
      }

      val names = arrayOf("sponge", null)
      val name = names[1] ?: "unknown name"

      println(name) // unknown name

 

'Self-study > Kotlin' 카테고리의 다른 글

[Kotlin] Basics  (0) 2024.01.12