5.3.4 List元素操作函数
add remove set clear
这两个添加、删除操作函数是MutableList里面的。跟Java中的集合类操作类似。
创建一个可变集合:
1. >>> val mutableList = mutableListOf(1,2,3)
2. >>> mutableList
3. [1, 2, 3]
向集合中添加一个元素:
1. >>> mutableList.add(4)
2. true
3. >>> mutableList
4. [1, 2, 3, 4]
在下标为0的位置添加元素0 :
1. >>> mutableList.add(0,0)
2. >>> mutableList
3. [0, 1, 2, 3, 4]
删除元素1 :
1. >>> mutableList.remove(1)
2. true
3. >>> mutableList
4. [0, 2, 3, 4]
5. >>> mutableList.remove(1)
6. false
删除下标为1的元素:
1. >>> mutableList.removeAt(1)
2. 2
3. >>> mutableList
4. [0, 3, 4]
删除子集合:
1. >>> mutableList.removeAll(listOf(3,4))
2. true
3. >>> mutableList
4. [0]
添加子集合:
1. >>> mutableList.addAll(listOf(1,2,3))
2. true
3. >>> mutableList
4. [1, 2, 3]
更新设置下标0的元素值为100:
1. >>> mutableList.set(0,100)
2. 0
3. >>> mutableList
4. [100]
清空集合:
1. >>> mutableList.clear()
2. >>> mutableList
3. []
把可变集合转为不可变集合:
1. >>> mutableList.toList()
2. [1, 2, 3]
3. ```#### `retainAll`
5. 取两个集合交集:
7. ```kotlin
8. >>> val mlist1 = mutableListOf(1,2,3,4,5,6)
9. >>> val mlist2 = mutableListOf(3,4,5,6,7,8,9)
10. >>> mlist1.retainAll(mlist2)
11. true
12. >>> mlist1
13. [3, 4, 5, 6]
15. ```#### `contains(element: T): Boolean`
17. 判断集合中是否有指定元素,有就返回true,否则返回false 。
18. 代码示例:
20. ```kotlin
21. >>> val list = listOf(1,2,3,4,5,6,7)
22. >>> list.contains(1)
23. true
24. ```#### `elementAt(index: Int): T`
26. 查找下标对应的元素,如果下标越界会抛IndexOutOfBoundsException。
27. 代码示例:
29. ```kotlin
30. >>> val list = listOf(1,2,3,4,5,6,7)
31. >>> list.elementAt(6)
32. 7
33. >>> list.elementAt(7)
34. java.lang.ArrayIndexOutOfBoundsException: 7
35. at java.util.Arrays$ArrayList.get(Arrays.java:3841)
另外,针对越界的处理,还有下面两个函数:
elementAtOrElse(index: Int, defaultValue: (Int) -> T): T : 查找下标对应元素,如果越界会根据方法返回默认值。
1. >>> list.elementAtOrElse(7,{0})
2. 0
3. >>> list.elementAtOrElse(7,{10})
4. 10
elementAtOrNull(index: Int): T? : 查找下标对应元素,如果越界就返回null
1. >>> list.elementAtOrNull(7)
2. null
3. ```#### `first()`
5. 返回集合第1个元素,如果是空集,抛出异常NoSuchElementException。
7. ```kotlin
8. >>> val list = listOf(1,2,3)
9. >>> list.first()
10. 1
11. >>> val emptyList = listOf<Int>()
12. >>> emptyList.first()
13. java.util.NoSuchElementException: List is empty.
14. at kotlin.collections.CollectionsKt___CollectionsKt.first(_Collections.kt:178)
对应的有针对异常处理的函数firstOrNull(): T? :
1. >>> emptyList.firstOrNull()
2. null
3. ```#### `first(predicate: (T) -> Boolean): T`
5. 返回符合条件的第一个元素,没有则抛异常NoSuchElementException 。
7. ```kotlin
8. >>> val list = listOf(1,2,3)
9. >>> list.first({it%2==0})
10. 2
11. >>> list.first({it>100})
12. java.util.NoSuchElementException: Collection contains no element matching the predicate.
对应的有针对异常处理的函数firstOrNull(predicate: (T) -> Boolean): T? ,返回符合条件的第一个元素,没有就返回null :
1. >>> list.firstOrNull({it>100})
2. null
3. ```#### `indexOf(element: T): Int`
5. 返回指定下标的元素,没有就返回-1
7. ```kotlin
8. >>> val list = listOf("a","b","c")
9. >>> list.indexOf("c")
10. 2
11. >>> list.indexOf("x")
12. -1
13. ```#### `indexOfFirst(predicate: (T) -> Boolean): Int`
15. 返回第一个符合条件的元素下标,没有就返回-1 。
17. ```kotlin
18. >>> val list = listOf("abc","xyz","xjk","pqk")
19. >>> list.indexOfFirst({it.contains("x")})
20. 1
21. >>> list.indexOfFirst({it.contains("k")})
22. 2
23. >>> list.indexOfFirst({it.contains("e")})
24. -1
25. ```#### `indexOfLast(predicate: (T) -> Boolean): Int`
27. 返回最后一个符合条件的元素下标,没有就返回-1 。
29. ```kotlin
30. >>> val list = listOf("abc","xyz","xjk","pqk")
31. >>> list.indexOfLast({it.contains("x")})
32. 2
33. >>> list.indexOfLast({it.contains("k")})
34. 3
35. >>> list.indexOfLast({it.contains("e")})
36. -1
37. ```#### `last()`
39. 返回集合最后一个元素,空集则抛出异常NoSuchElementException。
41. ```kotlin
42. >>> val list = listOf(1,2,3,4,7,5,6,7,8)
43. >>> list.last()
44. 8
45. >>> val emptyList = listOf<Int>()
46. >>> emptyList.last()
47. java.util.NoSuchElementException: List is empty.
48. at kotlin.collections.CollectionsKt___CollectionsKt.last(_Collections.kt:340)
49. ```#### `last(predicate: (T) -> Boolean): T`
51. 返回符合条件的最后一个元素,没有就抛NoSuchElementException
53. ```kotlin
54. >>> val list = listOf(1,2,3,4,7,5,6,7,8)
55. >>> list.last({it==7})
56. 7
57. >>> list.last({it>10})
58. java.util.NoSuchElementException: List contains no element matching the predicate.
对应的针对越界处理的lastOrNull函数:返回符合条件的最后一个元素,没有则返回null :
1. >>> list.lastOrNull({it>10})
2. null
3. ```#### `lastIndexOf(element: T): Int`
5. 返回符合条件的最后一个元素,没有就返回-1
7. ```kotlin
8. >>> val list = listOf("abc","dfg","jkl","abc","bbc","wer")
9. >>> list.lastIndexOf("abc")
10. 3
11. ```#### `single(): T`
13. 该集合如果只有1个元素,则返回该元素。否则,抛异常。
15. ```kotlin
16. >>> val list = listOf(1)
17. >>> list.single()
18. 1
20. >>> val list = listOf(1,2)
21. >>> list.single()
22. java.lang.IllegalArgumentException: List has more than one element.
23. at kotlin.collections.CollectionsKt___CollectionsKt.single(_Collections.kt:471)
25. >>> val list = listOf<Int>()
27. >>> list.single()
28. java.util.NoSuchElementException: List is empty.
29. at kotlin.collections.CollectionsKt___CollectionsKt.single(_Collections.kt:469)
30. ```#### `single(predicate: (T) -> Boolean): T`
32. 返回符合条件的单个元素,如有没有符合的抛异常NoSuchElementException,或超过一个的抛异常IllegalArgumentException。
34. ```kotlin
35. >>> val list = listOf(1,2,3,4,7,5,6,7,8)
36. >>> list.single({it==1})
37. 1
38. >>> list.single({it==7})
39. java.lang.IllegalArgumentException: Collection contains more than one matching element.
41. >>> list.single({it==10})
42. java.util.NoSuchElementException: Collection contains no element matching the predicate.
对应的针对异常处理的函数singleOrNull: 返回符合条件的单个元素,如有没有符合或超过一个,返回null
1. >>> list.singleOrNull({it==7})
2. null
3. >>> list.singleOrNull({it==10})
4. null
