kotlin——数组(增删改查)、haspMap(增删改查过滤)、for中断continue
生活随笔
收集整理的這篇文章主要介紹了
kotlin——数组(增删改查)、haspMap(增删改查过滤)、for中断continue
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、數組的增刪改查
1、數組遍歷(查)
val array: Array<String> = Array(5) { (it * 10).toString() } // 下標for (item in array.indices) {println("下標為==>" + item)} // 項的內容for (item in array) {println("項為==>" + item)} // 下標和項for ((item,i) in array.withIndex()) {println("項為==> $item;下標為==>$i")}另一種寫法(推薦):
val array: Array<String> = Array(5) { (it * 10).toString() }array.forEach {println("項的內容為:$it")}array.forEachIndexed { index, item ->println("下標為:$index;項的內容為:$item")}A、使用map形式遍歷(給數組的值都乘以2)
val num = listOf(1, 2, 3, 4, 5)val collect1 = num.stream().map { n: Int -> n * 2 }.collect(Collectors.toList())println(collect1) //[2, 4, 6, 8, 10]B、使用filter形式遍歷(找出大于2的數)
val num = listOf(1, 2, 3, 4, 5)val collect1 = num.stream().filter() { n: Int -> n > 2 }.collect(Collectors.toList())println(collect1) //[3,4,5]C、使用anyMatch遍歷(找出里面是否有2的值,返回boolean類型)
val num = listOf(1, 2, 3, 4, 5) val collect1 = num.stream().anyMatch { n: Int -> n == 2 } println(collect1) // trueD、檢測state是否全部是2(返回true或false)
?
2、數組添加元素(不能add的解決方法)
在數組結尾加一個元素
var array: Array<String> = Array(5) { (it * 10).toString() }val array1 = array.plus("100")下面來說說不能add的解決方法:
var adapterList11: List<Int> = ArrayList() // 此處adapterList11是不能直接使用add方法的val mIntList = mutableListOf<Int>()mIntList.add(1)mIntList.add(2)adapterList11 = mIntList3、數組刪除元素
刪除數組第一個元素
var array: Array<String> = Array(5) { (it * 10).toString() }val arr1=array.drop(1)刪除數組最后一個元素
var array: Array<String> = Array(5) { (it * 10).toString() }val arr1=array.dropLast(1)4、數組修改元素
var array: Array<String> = Array(5) { (it * 10).toString() }array[2]="99"二、map的增刪改查過濾
val hashMap = hashMapOf("Java" to 86, "Kotlin" to 92, "Go" to 78)1、hasMap增加元素
hashMap.put("uniapp",93)批量放入多個 key-value 對,putAll(from: Map<out K, V>): Unit
2、hasMap刪除元素
hashMap.remove("Go")刪除map里面的所有元素
hashMap.clear()3、hasMap修改元素
hashMap["Java"] = 884、hasMap遍歷所有元素
for ((key, value) in hashMap) {println("==>$key: $value")}5、過濾(filter、any、all)
//1、返回所有value大于90的數據val hashMap1 = hashMap.filter {it.value > 90//"o" in it.key //key里面包含有字母o}println("hashMap1的值為==>: $hashMap1")//{Kotlin=92}//2、map里面是否有92的數據,返回true或者falseval hashMap2 = hashMap.any {it.value == 92}println("hashMap2的值為==>: $hashMap2")//true//3、 map.all 如果所有的元素都滿足給定的條件,則返回 trueval hashMap3 = hashMap.all {it.value < 100 && it.value > 60}println("hashMap3的值為==>: $hashMap3")//true三、for循環中斷
1、return中斷循環
fun test3() {val list = ArrayList<Int>()list.add(1)list.add(2)list.add(3)list.add(4)run outside@{list.forEachIndexed { index, i ->println("-------------$index--------$i") if (index == 2) { // 2改為5,就可以運行 “結束1” 這行輸出代碼,不然就會跳到outside標簽return@outside}}println("====結束1====")}println("====結束2====")}運行結果:
如果中斷用的是return@test3,這個日志“====結束2====”就不會運行2、continue中斷當前次循環
fun test3() {out@ for (i in 0..2) {outj@ for (j in 0..2) {if (j == 1) continue@outjprintln("j ===》 $j")// 1的時候就不運行}}}日志
總結
以上是生活随笔為你收集整理的kotlin——数组(增删改查)、haspMap(增删改查过滤)、for中断continue的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 转Draren总结的ollydbg v1
- 下一篇: 我们用ESP32-Wrover-Kit做