# 1. Language/🔰 Kotlin

Kotlin # 배열/콜렉션/이터러블

둥굴둥굴둥굴레차 2022. 7. 1. 13:49

🎈 배열

//배열 선언 방법
var array1 = arrayOf(true, false, "안녕", 10)

for(i in array1) println(i)

println("////////////")

var array2 = arrayOf<Int>(1, 2, 3)

for(i in array2) println(i)

println("////////////")

var array3 = intArrayOf(1, 2, 3)

for(i in array3) println(i)

println("////////////")

var array4 = Array(10, {0})

for(i in array4) println(i)

println("////////////")

var array5 = Array<Int>(10, {1})

for(i in array5) println(i)

println("////////////")


// 배열의 인자로 변수를 넣는 방법
val score1: Int = 100
val score2: Int = 200
val score3: Int = 300

var scoreArray = arrayOf<Int>(score1, score2, score3)
println(scoreArray.get(0))

 

 

🎈 콜렉션

 

// List - Immutable
val numbers = listOf<Int>(1, 2, 3)
//numbers[0] = 2 //-> 블가능. error: no set method providing array access. 불변하는 리스트기 때문에 set메서드를 지원하지 않음!
println(numbers)
println("/////////////////")

val NUMS: List<Int> = listOf<Int>(1, 2, 3)

for(i in NUMS) println(i)

println("/////////////////")

println(numbers[0])
println(numbers.get(0))
println(numbers.indexOf(1))
println(numbers.last())
println(numbers.first())
println("/////////////////")

// List - mutable
val numbers2 = mutableListOf<Int>(1, 2, 3)
numbers2[0] = 2
println(numbers2)
println("/////////////////")

numbers2.add(55)
println(numbers2)

numbers2.removeAt(0)
println(numbers2)
println("/////////////////")

// Set - Immutable
val numbers3 = setOf<Int>(1, 1, 2, 3)
println(numbers3)
println("/////////////////")

val NUMS2: Set<Int> = setOf<Int>(1, 2, 3)

for(i in NUMS2) println(i)

println("/////////////////")

// Set - mutable
val numbers4 = mutableSetOf<Int>(1, 1, 2, 3)
println(numbers4)
println("/////////////////")


// Map - Immutable
val numbers5 = mapOf<Int, String>(1 to "하나", 2 to "둘")
println(numbers5)
println("/////////////////")

val numbers6 = mapOf<Int, String>(Pair(1,  "하나"), Pair(2, "둘"))
println(numbers6)
println("/////////////////")

val NUMS3: Map<Int, String> = mapOf<Int, String>(1 to "하나", 2 to "둘")

println(NUMS3)
println("/////////////////")

// Map - mutable
val numbers7 = mutableMapOf<Int, String>(1 to "하나", 2 to "둘")
println(numbers7)

 

 

🎈 이터러블

 

//Iterable

// Range
val range1 = 1..10
println(range1)

val range2 = 1 until 11
println(range2)

val range3 = 'A'..'Z' //-> 코틀린은 영어와 한글의 순서를 알고있다.
println(range3)