iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 8
1

List (不能修改)

List 語法


  listOf(1,2,3)

MutableList (能修改)

表示 特定位置的元素

 
fun main (){

val list = mutableListOf(1,2,3)
// 表示 特定位置的元素
val element = list[1]
 
println(element)

// result:
//2
}
 

add

加入 新元素


fun main (){

val list = mutableListOf(1,2,3)
list .add (4)
println(list)

// result:
//[1, 2, 3, 4]
}

addAll

A MutableList 加入 B MutableList 中 所包含的元素


fun main(){
    
    var mutableList: MutableList<Int>  = mutableListOf(1,2,3)
    
    var mutableList2: MutableList<Int>  = mutableListOf(1,2 )
    
        mutableList.addAll(mutableList2)
      
        println(mutableList )

// result
// [1, 2, 3, 1, 2]

}

remove

移除特定元素


fun main (){

val list = mutableListOf(1,2,3)
list .remove (2)
println(list)

// result:
//[ 1, 3]
}

removeAll

A MutableList 移除掉 B MutableList 中 所包含的元素


fun main(){
    
    var mutableList: MutableList<Int>  = mutableListOf(1,2,3)
    
    var mutableList2: MutableList<Int>  = mutableListOf(1,2 )
    
        mutableList.removeAll(mutableList2)
      
        println(mutableList )

// result
// [3]

}


fun main(){
    
    var mutableList: MutableList<Int>  = mutableListOf(1,2,3)
    
    var mutableList2: MutableList<Int>  = mutableListOf(1,2,3)
    
        mutableList.removeAll(mutableList2)
      
        println(mutableList )

// result
// []

}

removeAt

移除特定位置的元素


fun main(){
    
    var mutableList: MutableList<Int>  = mutableListOf(1,2,3)
    
        mutableList.removeAt(0)
      
        println(mutableList )

}

// result:
//[2, 3]

clear

清空所有的元素


fun main(){
    
    var mutableList: MutableList<Int>  = mutableListOf(1,2,3)
    
        mutableList.clear()
      
        println(mutableList )

}

// result:
//[]

set

更改特定位置元素


fun main(){
    
    var mutableList: MutableList<Int>  = mutableListOf(1,2,3)
    
        mutableList.set(2,100)
      
        println(mutableList )

}

// result
// [1, 2, 100]

相當於下面


fun main(){
    
    var mutableList: MutableList<Int>  = mutableListOf(1,2,3)
    
        mutableList[2] = 100
      
        println(mutableList )

}

// result
// [1, 2, 100]

contains

mutablList 是否 包含特定 元素 有包含為 true 不包含則為 false

有包含為 true


fun main(){
    
    var mutableList: MutableList<Int>  = mutableListOf(1,2,3)
    val isExistOrNot = mutableList.contains(2)
      
        println(isExistOrNot )

}

不包含則為 false


fun main(){
    
    var mutableList: MutableList<Int>  = mutableListOf(1,2,3)
    val isExistOrNot = mutableList.contains(4)
      
        println(isExistOrNot )

}


containsAll

A mutableList 是否 包含 B mutableList 中所有的元素 有為 true 否則為 false


fun main(){
    
    var mutableList = mutableListOf<Int> (1,2,3)
    var mutableList2 = mutableListOf<Int> ( 2,3)
    val isExistOrNot = mutableList.containsAll(mutableList2)
      
        println(isExistOrNot )

}
// result
//true


fun main(){
    
    var mutableList = mutableListOf<Int> (1,2,3)
    var mutableList2 = mutableListOf<Int> ( 0)
    val isExistOrNot = mutableList.containsAll(mutableList2)
      
        println(isExistOrNot )

}
// result
//false

isEmpty

判斷集合是否為 空 則為 true 否則為 false


fun main(){
    
    var mutableList = mutableListOf<Int> (1,2,3)
 
        println(mutableList.isEmpty() )

}

// result
// false


fun main(){
    
    var mutableList = mutableListOf<Int> ()
 
        println(mutableList.isEmpty() )

}

// result
// true


上一篇
[Day 7] Kotlin Collection - Map
下一篇
[Day 9] Kotlin Collection - Set
系列文
Android 菜鳥村-開發基礎 30篇32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言