ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

kotlin操作符,kotlin实例化

2021-12-29 17:04:25  阅读:185  来源: 互联网

标签:返回 index val kotlin list element Returns 实例 操作符


val dropwhile=list.dropWhile {it > 1 }

/**

  • dropLastWhile 返回根据特定的函数 从最后一项开始 直到不满足条件后返回 列表
  • Returns a list containing all elements except last elements that satisfy the given [predicate].
  • 返回[0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0]
    */
    val dropLastWhile= list.dropLastWhile { it>4 }

/**
*filter 返回所有符合给定函数条件的元素。

  • Returns a list containing only elements matching the given [predicate].
  • [5, 6, 5]
    */
    val filter=list.filter { it>4 }

/**

  • filterNot 返回所有不符合给定函数条件的元素
  • Returns a list containing all elements not matching the given [predicate].
  • [0, 1, 2, 3, 4, 4, 3, 2, 1, 0]
    */
    val filterNot=list.filterNot { it>4 }

/**

  • filterNotNull 返回非null元素
  • Returns a list containing all elements that are not null.
  • [0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0]
    */
    val filterNotNull= list.filterNotNull()

/**

  • 返回满足该ranger的元素集合
  • Returns a list containing elements at indices in the specified [indices] range.
  • [0, 1, 2, 3, 4, 5, 6]
    */
    val slice= list.slice(0…6)

/**

  • listOf(0,4,7)是集合list的坐标
  • Returns a list containing elements at specified [indices].
  • [0, 4, 5]
    */
    val slice2= list.slice(listOf(0,4,7))

/**
*返回前n项

  • Returns a list containing first [n] elements.
  • [0, 1, 2, 3]
    */
    val take= list.take(4)

/**

  • 返回后n项
  • Returns a list containing last [n] elements.
  • [3, 2, 1, 0]
    */
    val takeLast= list.takeLast(4)

/**

  • 从第一项开始判断,直到不符合就返回,返回符合的前几项数据
  • Returns a list containing first elements satisfying the given [predicate].
  • []
    */
    val takeWhile= list.takeWhile { it>4 }
  • 映射操作符

private val list= listOf(0,1,2,3,4,5,4,3,2,1,0,-1)

/**

  • 返回满足条件的集合
  • Returns a list containing the results of applyin

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

g the given [transform] function

  • to each element in the original collection.
  • [false, false, false, true, true, true, true, true, false, false, false, false]
    */
    val map=list.map { it>2 }

/**

  • 返回特定函数后的集合,参数是Iterable类型,
  • Returns a single list of all elements yielded from results of [transform]
  • function being invoked on each element of original collection.
  • [0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0, 1, -1, 0]
    */
    val flatMap=list.flatMap { listOf(it, it + 1) }

/**

  • 根据函数将集合分组,返回map类型对象
  • Groups elements of the original collection by the key returned by the given [keySelector] function
  • applied to each element and returns a map where each group key is associated with a list of corresponding elements.
  • The returned map preserves the entry iteration order of the keys produced from the original collection.
  • {false=[0, 1, 2, 3, 3, 2, 1, 0, -1], true=[4, 5, 4]}
  • @sample samples.collections.Collections.Transformations.groupBy
    */
    val groupBy=list.groupBy {value-> value>3 }

/**

  • 返回一个集合,通过 角标和值 来生成
  • Returns a list containing the results of applying the given [transform] function
  • to each element and its index in the original collection.
  • @param [transform] function that takes the index of an element and the element itself
  • and returns the result of the transform applied to the element.
  • [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0, -1]
    */
    val mapIndexed=list.mapIndexed { index, value ->value}

/**

  • 返回一个每一个非null元素根据给定的函数转换所组成的List
  • Returns a list containing only the non-null results of applying the given [transform] function
  • to each element in the original collection.
  • [0, 2, 4, 6, 8, 10, 8, 6, 4, 2, 0, -2]
    /
    val mapNotNull=list.mapNotNull { it
    2 }
  • 元素操作符

private val list= listOf(0,1,2,3,4,5,6,4,3,2,1,0,-1)

//如果指定元素可以在集合中找到,则返回true。
val contains=list.contains(2)

/**

  • 返回给定index对应的元素,如果index数组越界则会 抛出IndexOutOfBoundsException
  • Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection.
  • 2
    */
    val elementAt=list.elementAt(2)

/**

  • 返回给定index对应的元素,如果index数组越界则会根据给定函数返回默认值,第二个参数default,lamdba表达式
  • Returns an element at the given [index] or the result of calling the [defaultValue]
  • function if the [index] is out of bounds of this collection.
  • 2
    */
    val elementAtOrElse=list.elementAtOrElse(2){“error”}

/**

  • 返回给定index对应的元素,如果index数组越界则会 返回null
  • Returns an element at the given [index] or null if the [index] is out of bounds of this list.
  • null
    */
    val elementAtOrNull=list.elementAtOrNull(19)

/**

  • Returns first element.
  • @throws [NoSuchElementException] if the list is empty.
  • 0
    */
    val first=list.first()

/**

  • 返回符合给定函数条件的第一个元素,没有回抛异常
  • Returns the first element matching the given [predicate].
  • @throws [NoSuchElementException] if no such element is found.
  • 4
    */
    val first2=list.first { it>3 }

/**

  • 返回符合给定函数条件的第一个元素,如果没有符合则返回null
  • Returns the first element matching the given [predicate], or null if element was not found.
  • null
    */
    val firstOrNull=list.firstOrNull { it>9 }

/**

  • 返回指定元素的第一个index,如果不存在,则返回-1
  • Returns the index of the first occurrence of the specified element in the list, or -1 if the specified
  • element is not contained in the list.
  • 3
    */
    val indexOf=list.indexOf(3)

/**

  • 返回第一个符合给定函数条件的元素的index,如果没有符合则返回-1
  • Returns index of the first element matching the given [predicate], or -1 if the list does not contain such element.
  • 0
    */
    val indexOfFirst=list.indexOfFirst { it%3==0 }

/**

  • 返回最后一个符合给定函数条件的元素的index,如果没有符合则返回-1
  • Returns index of the last element matching the given [predicate], or -1 if the list does not contain such element.
  • 11
    */
    val indexOfLast=list.indexOfLast { it%3==0 }

/**

  • 返回符合给定函数条件的最后一个元素,没有抛异常
  • Returns the last element matching the given [predicate].
  • @throws [NoSuchElementException] if no such element is found.
  • 6
    */
    val last=list.last { it>4 }

/**

  • 返回指定元素的最后一个index,如果不存在,则返回-1
  • Returns the index of the last occurrence of the specified element in the list, or -1 if the specified
  • element is not contained in the list.
  • 8
    */
    val lastIndexOf=list.lastIndexOf(3)

/**

  • 返回符合给定函数条件的最后一个元素,如果没有符合则返回null
  • Returns the last element matching the given [predicate], or null if no such element was found.
  • null
    */
    val lastOrNull=list.lastOrNull { it>8 }

/**

  • 返回符合给定函数的单个元素,如果没有符合或者超过一个,则抛出异常
  • Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element.
  • 6
    */
    val single=list.single { it>5 }

/**

  • 返回符合给定函数的单个元素,如果没有符合或者超过一个,则返回null
  • Returns the single element matching the given [predicate], or null if element was not found or more than one element was found.
  • null
    */
    val singleOrNull=list.singleOrNull { it>8 }
  • 生产操作符

private val list1= listOf(0,1,2,3,4,5)
private val list2= listOf(4,5,2,1,5)

/**

  • Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
  • [(0, 4), (1, 5), (2, 2), (3, 1), (4, 5)]
    */
    val zip= list1.zip(list2)

/**

  • Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
  • [4, 6, 4, 4, 9]
    */
    val zip2= list1.zip(list2){it1,it2->it1+it2}

//[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
val zip4= list1.zip(list1+list2)

//[0, 2, 4, 6, 8, 10]
val zip3= list1.zip(list1+list2){it1,it2->it1+it2}

/**

  • Returns a pair of lists, where
  • first list is built from the first values of each pair from this collection,

标签:返回,index,val,kotlin,list,element,Returns,实例,操作符
来源: https://blog.csdn.net/m0_65512599/article/details/122219827

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有