kotlin中的List集合分為可變和不可變,如果對集合中的數(shù)據(jù)沒有增、刪、改的需求,那么兩種方式都可用。但是如果有增、刪、改的需求,就只能聲明可變List(MutableList)
我們可以用listof()函數(shù)創(chuàng)建一個不可變List,它有3個重載函數(shù),創(chuàng)建的List都是不可變的
public inline fun <T> listOf(): List<T> = emptyList()
public fun <T> listOf(vararg elements: T): List<T> = if (elements.size > 0) elements.asList() else emptyList()
public fun <T> listOf(element: T): List<T> = java.util.Collections.singletonList(element)
我們可以點進listOf()函數(shù)的返回值類型List中查看一下源碼
public interface List<out E> : Collection<E> {
override val size: Int
override fun isEmpty(): Boolean
override fun contains(element: @UnsafeVariance E): Boolean
override fun iterator(): Iterator<E>
override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean
public operator fun get(index: Int): E
public fun indexOf(element: @UnsafeVariance E): Int
public fun lastIndexOf(element: @UnsafeVariance E): Int
public fun listIterator(): ListIterator<E>
public fun listIterator(index: Int): ListIterator<E>
public fun subList(fromIndex: Int, toIndex: Int): List<E>
}
從源碼中可以看出,不可變List只有查找函數(shù),而沒有add(),remove(),clear(),set()等修改集合元素的函數(shù)。所以在平時使用List集合的時候,需要根據(jù)自己的需求來聲明List集合。本人在學(xué)kotlin的過程中,就犯了這樣的錯誤,使用listof()函數(shù)創(chuàng)建了一個集合,卻怎么也找不到add()函數(shù),困惑了好久!
與不可變List相對應(yīng)的自然就是可變List(MutableList)了。在MutableList中,除了繼承List中的那些函數(shù)外,另外新增了add/addAll、remove/removeAll/removeAt、set、clear、retainAll等更新修改的操作函數(shù)。
創(chuàng)建一個MutableList的對象實例跟List類似,前面加上前綴 mutable
val mutableList = mutableListOf(1, 2, 3)
println(mutableList)
輸出:
[1, 2, 3]
另外,我們也可以直接使用Kotlin封裝的 arrayListOf 函數(shù)來創(chuàng)建一個可變List:
val arrayList= arrayListOf(1, 2, 3)
println(arrayList)
輸出:
[1, 2, 3]
雖然,kotlin將List集合分為可變和不可變,但是它們之間也是可以互相轉(zhuǎn)換的
//創(chuàng)建一個不可變List
val imList = listOf(4, 5, 6)
//將不可變List轉(zhuǎn)換為可變List
imList.toMutableList()
//創(chuàng)建一個可變List
val mutableList = mutableListOf(1, 2, 3)
//將一個可變List轉(zhuǎn)換為不可變List
mutableList.toList()
在kotlin里封裝了大量的方便操作List集合的函數(shù)
增、刪、改操作
// 創(chuàng)建一個可變集合:
val mutableList = mutableListOf(1, 2, 3)
// 向集合中添加一個元素:
mutableList.add(4)
//[1, 2, 3, 4]
//在下標(biāo)為0的位置添加元素0
mutableList.add(0, 0)
//[0, 1, 2, 3, 4]
//刪除元素1
mutableList.remove(1)
// [0, 2, 3, 4]
// 刪除下標(biāo)為1的元素
mutableList.removeAt(1)
// [0, 3, 4]
//刪除子集合
mutableList.removeAll(listOf(3, 4))
// [0]
// 添加子集合
mutableList.addAll(listOf(1, 2, 3))
// [0, 1, 2, 3]
// 更新設(shè)置下標(biāo)0的元素值為100
mutableList.set(0, 100)
//[100, 1, 2, 3]
//清空集合:
mutableList.clear()
// []
取兩個集合交集:
retainAll()
val mList1 = mutableListOf(1,2,3,4,5,6)
val mList2 = mutableListOf(3,4,5,6,7,8,9)
println(mList1.retainAll(mList2))
輸出
[3, 4, 5, 6]
判斷集合中是否有指定元素,有就返回true,否則返回false 。
contains()
list = listOf(1,2,3,4,5,6,7)
println(list.contains(1))
輸出
true
查找下標(biāo)對應(yīng)的元素
elementAt() 下標(biāo)越界會拋IndexOutOfBoundsException
elementAtOrElse(index: Int, defaultValue: (Int) -> T): T 下標(biāo)越界會將defaultValue返回。
elementAtOrNull(index: Int): T?下標(biāo) 越界返回null
示例代碼
val list = listOf(1,2,3,4,5,6,7)
println(list.elementAt(6))
輸出
7
list = listOf(1,2,3,4,5,6,7)
println(mutableList.elementAtOrElse(7) { 0 })
0
println(mutableList.elementAtOrNull(7))
null
獲取集合第一個元素
first() 如果是emptyList(),拋出NoSuchElementException
val list = listOf(1,2,3)
println(list.first())
輸出
1
val emptyList = listOf<Int>()
println(emptyList.first())
拋出異常
java.util.NoSuchElementException: List is empty.
firstOrNull() 如果是emptyList(),則返回null
emptyList.firstOrNull()
輸出
null
first(predicate: (T) -> Boolean): T 返回元素,沒有則拋NoSuchElementException
val list = listOf(1,2,3)
list.first({it%2==0})
輸出
2
list.first({it>100})
拋出異常
java.util.NoSuchElementException: Collection contains no element matching the predicate
firstOrNull(predicate: (T) -> Boolean): T? 返回,沒有就返null
list.firstOrNull({it>100})
輸出
null
獲取指定元素或者下標(biāo)
indexOf(element: T): Int 返回集合中指定元素的第一個匹配項的下標(biāo),沒有就返回-1
val mutableList = mutableListOf(1, 2, 1, 5, 3, 4, 5)
val indexOf = mutableList.indexOf(1)
println(indexOf)
輸出
0
indexOfFirst(predicate: (T) -> Boolean): Int 返回第一個符合給定predicate條件元素的下標(biāo),沒有就返回-1
val indexOfFirst = mutableList.indexOfFirst { it > 2 }
println(indexOfFirst)
輸出
3
indexOfLast(predicate: (T) -> Boolean): Int 返回與給定predicate函數(shù)匹配的最后一個元素的索引,如果沒有此元素,則返回-1
val indexOfLast = mutableList.indexOfLast { it > 4 }
println(indexOfLast)
輸出
6
lastIndexOf(element: T): Int 返回集合中指定元素最后一次出現(xiàn)的下標(biāo),如果沒有此元素則返回-1
val lastIndexOf= mutableList.lastIndexOf(1)
println(lastIndexOf)
輸出
2
last() 返回集合最后一個元素,emptyList則拋出異常NoSuchElementException。
val last = mutableList.last()
println(last)
輸出
5
val emptyList = emptyList<Int>()
println(emptyList.last())
拋出異常
NoSuchElementException: List is empty.
last(predicate: (T) -> Boolean): T 返回符合條件的最后一個元素,沒有就拋NoSuchElementException
val matchLast= mutableList.last { it > 4 }
println(matchLast)
輸出
5
val matchLast= mutableList.last { it > 5 }
println(matchLast)
拋出異常
java.util.NoSuchElementException: List contains no element matching the predicate.
lastOrNull(): T? 返回最后一個元素,如果是emptyList則返回null
val lastOrNull=mutableList.lastOrNull()
println(lastOrNull)
輸出
5
println(emptyList.lastOrNull())
輸出
null
lastOrNull(predicate: (T) -> Boolean): T? 返回符合條件的最后一個元素,沒有則返回null
val matchLastOrNull = mutableList.lastOrNull { it % 2 == 0 }
println(matchLastOrNull)
輸出
4
single(): T 返回單個元素,如果集合為空或包含多個元素,則拋出異常。
val singleList= listOf(99)
val single=singleList.single()
println(single)
輸出
99
val singleList= listOf(99,100)
val single=singleList.single()
拋出異常
java.lang.IllegalArgumentException: List has more than one element.
val emptyList = emptyList<Int>()
emptyList.single()
拋出異常
java.util.NoSuchElementException: List is empty.
singleOrNull(): T? 返回單個元素,如果集合為空或者包含多個元素則返回null
val singleList= listOf(99)
val single=singleList.singleOrNull()
println(single)
輸出
99
val singleList= listOf(99,100)
val single=singleList.singleOrNull()
輸出
null
val emptyList = emptyList<Int>()
emptyList.singleOrNull()
輸出
null
single(predicate: (T) -> Boolean): T 返回與給定predicate匹配的單個元素,如果沒有或有多個匹配元素,則拋出異常。
val singleList = listOf(89, 99, 100)
val matchSingle = singleList.single { it == 99 }
println(matchSingle)
輸出
99
//沒有匹配條件的元素
val singleList = listOf(89, 99, 100)
val matchSingle = singleList.single { it == 79 }
拋出異常
java.util.NoSuchElementException: Collection contains no element matching the predicate.
//有多個匹配條件的元素
val singleList = listOf(89,89, 99, 100)
val matchSingle = singleList.single { it == 89 }
拋出異常
java.lang.IllegalArgumentException: Collection contains more than one matching element.
singleOrNull(predicate: (T) -> Boolean): T? 返回與給定predicate匹配的單個元素,如果沒有或有多個匹配元素,返回null。
val singleList = listOf(89, 99, 100)
val matchSingle = singleList.single { it == 99 }
println(matchSingle)
輸出
99
//沒有匹配條件的元素
val singleList = listOf(89, 99, 100)
val matchSingle = singleList.single { it == 79 }
輸出
null
//有多個匹配條件的元素
val singleList = listOf(89,89, 99, 100)
val matchSingle = singleList.single { it == 89 }
輸出
null