kotlin练习 ---- 数组练习
数组创建
- 使用arrayOf()函数:这种方式无需显示指定数组的长度,但需要依次列出每个数组元素。
fun main(args: Array<String>) {
//创建包含指定元素的数组
var arg1 = arrayOf("Java","Kotlin","PHP","Swift")
var arg2 = arrayOf(0,1,2,3,4,5)
println("arg1 = ${Arrays.toString(arg1)}")
println("arg2 = ${Arrays.toString(arg2)}")
}
- 使用arrayOfNulls()函数:这种方法需要显示指定数组的长度,数组元素全部被初始化为null。
fun main(args: Array<String>) {
//创建指定长度,元素为null的数组
var arg3 = arrayOfNulls<Int>(5)
}
- 使用emptyArray()函数:这种方法会创建一个长度为0的空数组。
fun main(args: Array<String>) {
//创建长度为0的空数组
var arg4 = emptyArray<String>()
var arg5 = emptyArray<Int>()
}
- 使用Array(size : Int,init :(Int) -> T )构造器:这种方法需要显示指定数组的长度,并可通过Lambda表达式来动态计算各数组元素的值,
fun main(args: Array<String>) {
//创建指定长度,使用Lambda表达式初始化数组元素的数组
var arg6 = Array(5,{(it * 2+97).toChar()})
var arg7 = Array(6,{"fkit"})
println("arg6 = ${Arrays.toString(arg6)}")
println("arg7 = ${Arrays.toString(arg7)}")
}
- 其他 Array<T>:Kotlin专门提供了 ByteArray、ShortArray、IntArra、LongArray、CharArray、FloatArray、DoubleArray、BooleanArray 分别对应java中的 byte[]、short[]、int[]、long[]、char[]、floar[]、double[]、boolean[] 这8种基本数据类型的数组。
fun main(args: Array<String>) {
var intArr = intArrayOf(2,3,4,5,6)
var doubleArr = doubleArrayOf(2.1,2.2,2.3,2.4)
var intArr2 = IntArray(5,{it*it})
var charArr = CharArray(5,{(it*2+97).toChar()})
println("intArr = ${Arrays.toString(intArr)}")
println("doubleArr = ${Arrays.toString(doubleArr)}")
println("intArr2 = ${Arrays.toString(intArr2)}")
println("charArr = ${Arrays.toString(charArr)}")
}
数组使用
- get(index) 方法 ,set(index,value)方法
fun main(args: Array<String>) {
var strArray = arrayListOf<String>("Java","Kotlin","Go","Swift")
println(strArray[1])//Kotlin
println(strArray.get(1))//Kotlin
strArray.set(0,"Python")
strArray[2] = "Groovy"
println(strArray.toString())//[Python, Kotlin, Groovy, Swift]
}
fun main(args: Array<String>) {
var books = arrayListOf<String>("疯狂java讲义", "疯狂android讲义", "疯狂kotlin讲义")
for (book in books) {
println(book)
}
for (i in 0 until books.size) {
println(books[i])
}
//根据数组的索引来遍历数组
for (i in books.indices){//索引 = size -1
println(books[i])
}
for ((index,value) in books.withIndex()){//索引 = size -1
println("索引为${index}的元素为:${value}")
}
}
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)]
网友评论