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`
  4. 取两个集合交集:
  5. ```kotlin
  6. >>> val mlist1 = mutableListOf(1,2,3,4,5,6)
  7. >>> val mlist2 = mutableListOf(3,4,5,6,7,8,9)
  8. >>> mlist1.retainAll(mlist2)
  9. true
  10. >>> mlist1
  11. [3, 4, 5, 6]
  12. ```#### `contains(element: T): Boolean`
  13. 判断集合中是否有指定元素,有就返回true,否则返回false 。
  14. 代码示例:
  15. ```kotlin
  16. >>> val list = listOf(1,2,3,4,5,6,7)
  17. >>> list.contains(1)
  18. true
  19. ```#### `elementAt(index: Int): T`
  20. 查找下标对应的元素,如果下标越界会抛IndexOutOfBoundsException。
  21. 代码示例:
  22. ```kotlin
  23. >>> val list = listOf(1,2,3,4,5,6,7)
  24. >>> list.elementAt(6)
  25. 7
  26. >>> list.elementAt(7)
  27. java.lang.ArrayIndexOutOfBoundsException: 7
  28. 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()`
  4. 返回集合第1个元素,如果是空集,抛出异常NoSuchElementException。
  5. ```kotlin
  6. >>> val list = listOf(1,2,3)
  7. >>> list.first()
  8. 1
  9. >>> val emptyList = listOf<Int>()
  10. >>> emptyList.first()
  11. java.util.NoSuchElementException: List is empty.
  12. at kotlin.collections.CollectionsKt___CollectionsKt.first(_Collections.kt:178)

对应的有针对异常处理的函数firstOrNull(): T? :

  1. >>> emptyList.firstOrNull()
  2. null
  3. ```#### `first(predicate: (T) -> Boolean): T`
  4. 返回符合条件的第一个元素,没有则抛异常NoSuchElementException 。
  5. ```kotlin
  6. >>> val list = listOf(1,2,3)
  7. >>> list.first({it%2==0})
  8. 2
  9. >>> list.first({it>100})
  10. 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`
  4. 返回指定下标的元素,没有就返回-1
  5. ```kotlin
  6. >>> val list = listOf("a","b","c")
  7. >>> list.indexOf("c")
  8. 2
  9. >>> list.indexOf("x")
  10. -1
  11. ```#### `indexOfFirst(predicate: (T) -> Boolean): Int`
  12. 返回第一个符合条件的元素下标,没有就返回-1 。
  13. ```kotlin
  14. >>> val list = listOf("abc","xyz","xjk","pqk")
  15. >>> list.indexOfFirst({it.contains("x")})
  16. 1
  17. >>> list.indexOfFirst({it.contains("k")})
  18. 2
  19. >>> list.indexOfFirst({it.contains("e")})
  20. -1
  21. ```#### `indexOfLast(predicate: (T) -> Boolean): Int`
  22. 返回最后一个符合条件的元素下标,没有就返回-1 。
  23. ```kotlin
  24. >>> val list = listOf("abc","xyz","xjk","pqk")
  25. >>> list.indexOfLast({it.contains("x")})
  26. 2
  27. >>> list.indexOfLast({it.contains("k")})
  28. 3
  29. >>> list.indexOfLast({it.contains("e")})
  30. -1
  31. ```#### `last()`
  32. 返回集合最后一个元素,空集则抛出异常NoSuchElementException。
  33. ```kotlin
  34. >>> val list = listOf(1,2,3,4,7,5,6,7,8)
  35. >>> list.last()
  36. 8
  37. >>> val emptyList = listOf<Int>()
  38. >>> emptyList.last()
  39. java.util.NoSuchElementException: List is empty.
  40. at kotlin.collections.CollectionsKt___CollectionsKt.last(_Collections.kt:340)
  41. ```#### `last(predicate: (T) -> Boolean): T`
  42. 返回符合条件的最后一个元素,没有就抛NoSuchElementException
  43. ```kotlin
  44. >>> val list = listOf(1,2,3,4,7,5,6,7,8)
  45. >>> list.last({it==7})
  46. 7
  47. >>> list.last({it>10})
  48. java.util.NoSuchElementException: List contains no element matching the predicate.

对应的针对越界处理的lastOrNull函数:返回符合条件的最后一个元素,没有则返回null :

  1. >>> list.lastOrNull({it>10})
  2. null
  3. ```#### `lastIndexOf(element: T): Int`
  4. 返回符合条件的最后一个元素,没有就返回-1
  5. ```kotlin
  6. >>> val list = listOf("abc","dfg","jkl","abc","bbc","wer")
  7. >>> list.lastIndexOf("abc")
  8. 3
  9. ```#### `single(): T`
  10. 该集合如果只有1个元素,则返回该元素。否则,抛异常。
  11. ```kotlin
  12. >>> val list = listOf(1)
  13. >>> list.single()
  14. 1
  15. >>> val list = listOf(1,2)
  16. >>> list.single()
  17. java.lang.IllegalArgumentException: List has more than one element.
  18. at kotlin.collections.CollectionsKt___CollectionsKt.single(_Collections.kt:471)
  19. >>> val list = listOf<Int>()
  20. >>> list.single()
  21. java.util.NoSuchElementException: List is empty.
  22. at kotlin.collections.CollectionsKt___CollectionsKt.single(_Collections.kt:469)
  23. ```#### `single(predicate: (T) -> Boolean): T`
  24. 返回符合条件的单个元素,如有没有符合的抛异常NoSuchElementException,或超过一个的抛异常IllegalArgumentException。
  25. ```kotlin
  26. >>> val list = listOf(1,2,3,4,7,5,6,7,8)
  27. >>> list.single({it==1})
  28. 1
  29. >>> list.single({it==7})
  30. java.lang.IllegalArgumentException: Collection contains more than one matching element.
  31. >>> list.single({it==10})
  32. 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