프로그래밍/Kotlin

[코틀린 / Kotlin] 컬렉션 _ List (add, remove, removeAt, contains, listOf ...)

pupu91 2022. 10. 20. 18:13
반응형

 

List

muableListOf : 변경 가능한 리스트를 선언하는 함수
fun main(){

    /* 처음 선언시에 비어있는 상태로 생성 된 리스트는 타입 정보를 유추할 수 없으므로
       리스트에 포함 될 자료의 타입을 명시해야 함*/

    var emptyMutableList: MutableList<String> = mutableListOf<String>()

    /* 리스트 생성 시 초기 값을 넣어 생성하게 되면 타입 추론이 가능하므로 명시하지 않아도 됨*/
    var mutableList = mutableListOf("사과", "바나나", "메론")
    
    println("mutableList default init : ${mutableList}")
}

출력 결과
mutableList default init : [사과, 바나나, 메론]

 

 

 

add : 요소 뒤 값 추가
    var mutableList = mutableListOf("사과", "바나나", "메론") 
    println("mutableList default init : ${mutableList}")
   
    mutableList.add("딸기")
    println("mutableList add element : ${mutableList}")
 
 출력 결과
 mutableList default init : [사과, 바나나, 메론]
 mutableList add element : [사과, 바나나, 메론, 딸기]

 

 

인덱스 위치 지정해서 값 변경
    mutableList.add(2, "수박")
    println("mutabelList add index : ${mutableList}")

출력 결과
mutabelList add index : [사과, 바나나, 수박, 메론, 딸기]

 

 

 

remove, removeAt : 요소 삭제
    mutableList.remove("메론")
    println("mutabelList remove element : ${mutableList}")
    
    mutableList.removeAt(1)
    println("mutabelList remove index : ${mutableList}")

출력 결과
mutabelList remove element : [사과, 바나나, 수박, 딸기]
mutabelList remove index : [사과, 수박, 딸기]

 

 

 

 

get : 값 추출
    var value1 = mutableList.get(1)
    println("mutableList get(1) : $value1")
   
   /* get 메소드를 사용하는 것보다 인덱스를 사용해서 가져오는 것이 보편적인 방법 */
     
   /* 인덱스 접근 연산자([]) : 값 추출 */      
    var value2 = mutableList[2]
    println("mutableList access index [2] : $value2")
    
출력 결과
mutableList get(1) : 수박
mutableList access index [2] : 딸기

 

반응형

 

 

리스트 순회
    println("mutableList loop : ")
    for(item in mutableList){
        print("$item")
    }
    println()

출력 결과
mutableList loop : 
사과수박딸기

 

 

 

 

contains: 요소 포함 여부
    println("mutableList contains 딸기 : ${mutableList.contains("딸기")}")
    println("mutableList contains 포도 : ${mutableList.contains("포도")}")

출력 결과
mutableList contains 딸기 : true
mutableList contains 포도 : false

 

 

 

size : 요소 객수
    println("mutableList size : ${mutableList.size}")

출력 결과
mutableList size : 3

 

 

 

 

 

 listOf : 변경 불가능한 리스트(immutableList)를 선언하는 함수
    var immutableList = listOf(1, 2, 3) //읽기 전용 리스트
   
    /* 삽입, 수정, 삭제가 불가능한 읽기 전용 리스트 */
    // immutableList.add()
    //immutableList.remove()
    //immutableList[0] = 0
    println("immutableList index [2] : ${immutableList[2]}")

    /* toMutabelList() 함수로 immutableList -> mutableList로 전환 가능 */
    var immutableToMutableList = immutableList.toMutableList()

    immutableToMutableList.add(4)
    immutableToMutableList.add(5)
    println("after transform to mutableList : $immutableToMutableList")

    /* toList() 함수로 mutableList -> immutableList로 전환 가능 */
    var mutableToImmutableList = immutableToMutableList.toList() //변경 불가한 읽기 전용 리스트로 변화 시킴

    //mutableToImmutableList.add(6) 값 추가 수정 삭제 불가
    
 출력 결과
 immutableList index [2] : 3
 after transform to mutableList : [1, 2, 3, 4, 5]

 

 

' + ' 연산자로 두 리스트를 합칠 수 있다.(동일한 타입만 가능)
   var plusList = listOf('a','b','c') + listOf('d','e','f')
    println("list concat: $plusList")

출력 결과
list concat: [a, b, c, d, e, f]

 

 

 

' - ' 연산자로 앞의 리스트에서 뒤 리스트의 내용을 삭제한 리스트를 얻을 수 있다.(중복 되어 있는 내용도 모두 삭제 됨)
    var subtractList = listOf(1, 2, 3, 1, 3, 4, 5, 2, 6) - listOf(2, 4, 6, 1)
    println("list minus : $subtractList")

출력 결과
list minus : [3, 3, 5]
반응형