배열과 리스트의 차이 [참고]
Arrays
var contacts = arrayOf("John", "James", "Amy")
List & MutableList (참고)
List : Read Only
MutableList : Read and Write
val mulist1: MutableList<Int> = mutableListOf(10, 20, 11, 23, 55)
val mulist2 = mutableListOf(10, 20, 30);
val mulist3 = (1..50).toMutableList()
val mulist4 = mutableListOf<Int>()
val mulist5 = MutableList<Int>(5, { i -> i})
val mulist: MutableList<Int> = mutableListOf<Int>(10, 20, 11, 23, 55)
mulist.add(500) //output : 10, 20, 11, 23, 55, 500
mulist.add(3, 111) //output : 10, 20, 11, 111, 23, 55, 500
val list = listOf<Int>(1, 2, 3)
mulist.addAll(2, list) //output : 10, 20, 1, 2, 3, 11, 111, 23, 55, 500
mulist.clear() //output : null
Map
val menu = mapOf()
val menu = mutableMapOf()
menu.put(string, object)
menu[string] = object
728x90
반응형
'Algorithm > Kotlin' 카테고리의 다른 글
Kotlin collections extension function (0) | 2021.10.24 |
---|---|
emptyList() (0) | 2021.08.05 |
Kotlin 조건문 (0) | 2021.04.30 |