1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| var indexs = arrayOf<Int>(1, 2, 3, 4, 5, 6, 7, 8, 9) //所有元素都要满足条件则返回 true var isAll = indexs.all { x -> x > 0 && x < 10 } print(isAll)//true //任一数组元素满足条件则返回 true var isAny = indexs.any { x -> x == 9 } println(isAny)//true //将数组转为List集合 var indexList = indexs.asList() println(indexList.toString()) //返回Map集合 var indexMap1 = indexs.associate { x -> x to x * x } println(indexMap1.toString())//{1=1, 2=4, 3=9, 4=16, 5=25, 6=36, 7=49, 8=64, 9=81} var indexMap2 = indexs.associate { it to it * it } println(indexMap2.toString())//{1=1, 2=4, 3=9, 4=16, 5=25, 6=36, 7=49, 8=64, 9=81} //返回Map集合 var indexMap3 = indexs.associateBy { it * it }//Map<K,T>中的K println(indexMap3.toString())//{1=1, 4=2, 9=3, 16=4, 25=5, 36=6, 49=7, 64=8, 81=9} var indexMap4 = indexs.associateBy({ it }, { it * it }) println(indexMap4.toString())//{1=1, 2=4, 3=9, 4=16, 5=25, 6=36, 7=49, 8=64, 9=81} var indexMap5 = indexs.associateBy({ it * it })//Map<K,T>中的K println(indexMap5.toString())//{1=1, 4=2, 9=3, 16=4, 25=5, 36=6, 49=7, 64=8, 81=9} //在将数组转成的Map集合插入map,最后返回修改后的map var map6 = mutableMapOf(10 to 100, 11 to 121, 12 to 144) var indexMap6 = indexs.associateByTo(map6, { it }, { it * it }) println(map6.toString())//{10=100, 11=121, 12=144, 1=1, 2=4, 3=9, 4=16, 5=25, 6=36, 7=49, 8=64, 9=81} //在将数组转成的Map集合插入map,最后返回修改后的map var map7 = mutableMapOf(10 to 100, 11 to 121, 12 to 144) var indexMap7 = indexs.associateTo(map7, { it to it * it }) println(map7.toString())//{10=100, 11=121, 12=144, 1=1, 2=4, 3=9, 4=16, 5=25, 6=36, 7=49, 8=64, 9=81} //数组平均值 var average = indexs.average(); println("平均值为$average")//5.0 //通过二分法查询element出现的索引,如果找不到则返回负数-------要求数组中的元素已经按升序排序 var binarySearch1 = indexs.binarySearch(4, 0, 3) println("4在数组(数组从第一个元素,到第三个元素)中的位置:$binarySearch1")//-4 var binarySearch2 = indexs.binarySearch(4) println("4在数组中的位置:$binarySearch2")//3 //判断数组中是否包含元素 var contains = indexs.contains(10); println("数组中是否有元素10:${contains}")//false
var indexs1 = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9) //判断两个数组是否相等 var contentDeepEquals = indexs.contentDeepEquals(indexs1) println("两个数组是否相等:${contentDeepEquals}")//true //将数组转成String var str_index = indexs.contentToString(); println(str_index)//[1, 2, 3, 4, 5, 6, 7, 8, 9] //复制数组成为一个新数组 var copyof1 = indexs.copyOf() println("copyof1 = ${copyof1.contentToString()}")//[1, 2, 3, 4, 5, 6, 7, 8, 9] indexs[0] = 0; println("copyof1 = ${copyof1.contentToString()}")//[1, 2, 3, 4, 5, 6, 7, 8, 9] indexs[0] = 1; var copyof2 = indexs.copyOf(5) println("copyof2 = ${copyof2.contentToString()}")//[1, 2, 3, 4, 5] var copyof3 = indexs.copyOf(10) println("copyof3 = ${copyof3.contentToString()}")//[1, 2, 3, 4, 5, 6, 7, 8, 9, null] //复制数组(通过索引指定) var copyOfRange1 = indexs.copyOfRange(0, 10) println("copyOfRange1 = ${copyOfRange1.contentToString()}")//[1, 2, 3, 4, 5, 6, 7, 8, 9, null] indexs[0] = 10 println("copyOfRange1 = ${copyOfRange1.contentToString()}")//[1, 2, 3, 4, 5, 6, 7, 8, 9, null] indexs[0] = 1 var copyOfRange2 = indexs.copyOfRange(0, 5) println("copyOfRange2 = ${copyOfRange2.contentToString()}")//[1, 2, 3, 4, 5] //数组满足条件的数量 var count1 = indexs.count({ it > 5 }) println("数组中大于5的元素有:${count1}个")//4
var indexs2 = arrayOf(1, 2, 3, 1, 4, 2, 3, 4, 1) //去掉数组中重复的元素 var indexs2_distinct1 = indexs2.distinct() println(indexs2_distinct1.toString())//[1, 2, 3, 4] var indexs2_distinct2 = indexs2.distinctBy { it % 2 == 0 } println(indexs2_distinct2.toString())//[1, 2]
//去掉数组中前3个元素 var drop = indexs.drop(3); println(drop.toString())
//去掉前面满足条件的元素,当出现第一个条件不满足时,返回后面的所有元素 var dropWhile = indexs.dropWhile { it <= 3 } println(dropWhile.toString())//[4, 5, 6, 7, 8, 9]
//去掉数组中后3个元素 var dropLast = indexs.dropLast(3); println(dropLast.toString())//[1, 2, 3, 4, 5, 6]
//去掉前面满足条件的元素,当出现第一个条件不满足时,返回前面的所有元素 var dropLastWhile = indexs.dropLastWhile { it > 6 } println(dropLastWhile.toString())//[1, 2, 3, 4, 5, 6]
//将数组中的元素赋值为element var indexs4 = arrayOf<Int>(1, 2, 3, 4, 5, 6, 7, 8, 9) indexs4.fill(3) println(indexs4.contentToString())//[3, 3, 3, 3, 3, 3, 3, 3, 3]
var indexs5 = arrayOf<Int>(1, 2, 3, 4, 5, 6, 7, 8, 9) indexs5.fill(3, 0, 5); println(indexs5.contentToString())//[3, 3, 3, 3, 3, 6, 7, 8, 9]
var first1 = indexs.first() println("数组的第一个元素:${first1}")//1 var first2 = indexs.first { it > 5 } println("数组的第一个满足条件的元素:${first2}")//6
var last1 = indexs.last() println("数组的最后一个元素:${last1}")//9 var last2 = indexs.last { it > 5 } println("数组的最后一个满足条件的元素:${last2}")//9
//求和 var fold = indexs.fold(0, { acc, i -> acc + i }) println("数组元素和为:${fold}") //搜索元素查询的位置 var index1 = indexs.indexOf(5)//从前往后 var index2 = indexs.lastIndexOf(5)//从后往前 println("元素5在数组中的位置是:${index1}和${index2}")
var indexs6 = arrayOf(1, 2, 3, 1, 4, 2, 3, 4, 1) var index3 = indexs6.indexOfFirst { it == 3 }//第一次出现的位置 var index4 = indexs6.indexOfLast { it == 3 }//最后一次查询的位置 println("元素3在数组中第一次出现在${index3},最后一次出现在${index4}")//2,6
var indexs7 = arrayListOf<Int>(3, 4, 5, 6, 7, 8, 9) var indexs8 = arrayListOf<Int>(1, 2, 5, 6) //intersect(交集),subtract(差集),union(并集),minus(补集) var intersect = indexs7.intersect(indexs8) var subtract = indexs7.subtract(indexs8) var union = indexs7.union(indexs8) var minus = indexs7.minus(indexs8) println("交集:${intersect}")//[5, 6] println("差集:${subtract}")//[3, 4, 7, 8, 9] println("并集:${union}")//[3, 4, 5, 6, 7, 8, 9, 1, 2] println("补集:${minus}")//[3, 4, 7, 8, 9] //数组最大值 var max = indexs.max();//9 //数组最小值 var min = indexs.min();//1 println("数组中的最大值:${max} 最小值:${min}")
var indexs9 = arrayListOf<Int>(1, 9, 3, 7, 2, 5, 4, 6, 8) //排序 indexs9.sort(); println(indexs9)//[1, 2, 3, 4, 5, 6, 7, 8, 9] //排序 val mapList = mutableListOf(1 to "A" , 2 to "B", 5 to "C", 3 to "D") mapList.sortBy { it.first } println(mapList) // [(1, A), (2, B), (3, D), (5, C)] mapList.sortBy { it.second } println(mapList) // [(1, A), (2, B), (5, C), (3, D)]
|