List, MutableList > IT 기술백서

IT 기술백서

직접 알아내거나 검색하기 귀찮아서 모아 둔 것

Android | List, MutableList

본문

List 는 한번 할당하면 크기와 값을 변경할 수 없다

MutableList 는 크기와 값을 변경할 수 있다.

 

사용예제

[code]

fun main() {

val a = listOf("사과", "딸기", "배")

    

    for (item in a) {

        println(item)

    }

    for (item in a) {

        println(item)

    }

 

    println()

    

    val b = mutableListOf(6,3,1)

    println(b)

    

    b.add(4)

    println(b)

    

    b.add(2, 8)

    println(b)

    

    b.removeAt(1)

    println(b)

    

    b.shuffle()

    println(b)

    

    b.sort()

    println(b)

[/code]

 

결과

[code]

사과

딸기

사과

딸기


[6, 3, 1]

[6, 3, 1, 4]

[6, 3, 8, 1, 4]

[6, 8, 1, 4]

[1, 4, 8, 6]

[1, 4, 6, 8]

[/code]

댓글 0개

등록된 댓글이 없습니다.

Menu