funmain(string:Array<String>){val test=listOf("AA","BB","CC",)println(test.elementAtOrNull(100))//没越界println(test.elementAtOrElse(2,defaultValue={"$it 这个下标不存在 "}))//越界后println(test.elementAtOrElse(22,defaultValue={"$it 这个下标不存在 "}))}//输出nullCC22这个下标不存在 ...
var list3 = list1.find { it it.startsWith("张") } println(list3) var list4 = list1.filter{it it.startsWith("张") } println(list4) var mu = mutableListOf() list1.filterTo(mu,{it it.startsWith("张") }) list2.filterTo(mu,{it it.startsWith("张") }) println(mu) //ind...
1. 集合 API 汇总: Iterable:任何实现此接口的集合,均可遍历元素。MutableIterable:继承 Iterable 接口,提供移除元素方法。Collection:元素通用集合,提供获取大小、判断是否为空、是否包含数据等方法。MutableCollection:继承 Collection 接口,提供额外的元素添加、删除等方法。List:继承 Collection 接...
一般使用标准库中的方法来快速创建集合, listOf()、setOf()、mutableListOf()、mutableSetOf() 当然,也可以使用Java的集合的构造方法 val numbers = listOf(0,1,2,3,4) 1. 空集合:emptyList()、emptySet() 与 emptyMap(),需要指定类型 val empty = emptyList<String>() 1. 取集合一部分 slice val ...
print("The index of last occurrence of element is : $index") } Output The index of last occurrence of element is : 3 Example 2: List.lastIndexOf() – Element not in the List In this example, we will try to find the output of List.lastIndexOf() function, when the element is not...
在Kotlin中,集合的初始化也有其特点。对于只读集合,通常使用listOf()方法初始化;对于可变集合,则使用mutableListOf()或直接创建ArrayList,这实际上是Java集合类的封装。在实际开发中,应优先使用只读集合,并在需要修改集合时才使用可变集合,以保持代码的清晰性和安全性。接下来,我们将对Kotlin中的...
[] 下标获取 List 集合元素外 , Kotlin 还提供了 安全获取集合元素的 方法 : getOrElse 和 getOrNull 函数..., 传入了两个参数 , 第一个参数是元素的索引 , 第二个参数是一个 Lambda 表达式 ; 该 Lambda 表达式的 Int 类型参数就是 index: Int 参数 ; 返回值分析 : 返回给定...{ for (element ...
layout.activity_main) showLongToast("hello") // 给Context类扩展了showToast方法,可以在任何有context的地方直接调用了 var mList = ArrayList<String>() mList.lastIndex // 任何ArrayList均可以调用该属性 ... } ... } 这里需要注意两点:1.扩展需要在包级范围内进行,如果写在class内是无效的。2.已经...
classCalculateFaceUtilprivateconstructor():CoroutineScopebyMainScope(){//... 单例/*** 计算并找到最匹对的人脸信息** 使用协程异步的并发的双端遍历查询最大值*/fungetTopFace(list:List<FaceRegisterInfo>,faceEngine:FaceEngine,faceFeature:FaceFeature,action:(similar:Float,index:Int)->Unit){// 、、、...
1.1.1、List转为Map val numbers =listOf("one","two","three","four")println(numbers.associateWith{ it.length}) 执行结果会转化为Map: {one=3,two=3,three=5,four=4} 1.2、Set Set内部是用Map实现的,Set相关的实现详见:Java Collection系列之:HashSet、LinkedHashSet、TreeSet的使用及源码解析 ...