코틀린 언어 정리 4-5

Collection operations

Common operations( 일반적인 연산들 )

Plus and Minus Operators

컬렉션에 요소, 또는 다른 컬렉션을 더하거나 빼는 기능을 수행합니다.

+, -, +=, -= 등의 연산자가 있습니다.


예제

fun test () {

   var list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

   println(list - list)

   println(list - (5..7))

   println(list - 1)

   println(list + list)

   println(list + 11)

   println(list + (11..20))


   val list_mutable = mutableListOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) // var로 선언할 경우 연산자 선택이 모호하여 컴파일 에러 발생

   list_mutable += 11

   println(list_mutable)

   list_mutable -= 11

   println(list_mutable)

   list_mutable += (11..20)

   println(list_mutable)

   list_mutable -= (11..20)

   println(list_mutable)

}




+ Recent posts