kotlin的数组
package com.lee.navdemo import kotlin.math.abs /** * * @ProjectName: NavDemo * @Package: com.lee.navdemo * @ClassName: KotlinBase * @Description: java类作用描述 * @Author: 李占伟 * @CreateDate: 21/3/11 下午9:37 * @UpdateUser: 更新者 * @UpdateDate: 21/3/11 下午9:37 * @UpdateRemark: 更新说明 * @Version: 1.0 */ fun main(){ println("------main--------") baseType() } fun baseType() { val num1 = -1.69 // Double val num2 = 2 //Int val num3 = 2f//Float val int1 = 3; println("num1:$num1 num2:$num2 num3:$num3 int1:$int1") println("num1 ab:${abs(num1)}") println(num1.toInt()) printType(num1) printType(num2) printType(num3) printType(int1) arrayType() } //kotlin的类型 fun printType(param:Any){ println("$param is ${param::class.simpleName} type") } /** * 数组 */ fun arrayType(){ //1 var array1= arrayOf(1,2,3)//通过arrayof创建数组 //2 val array2 = arrayOfNulls<Int>(3) array2[0] = 1 array2[1] = 2 array2[2] = 3 //3 var array3 = Array(5){i->(i*i).toString()} //4 // intArrayOf() // doubleArrayOf() val x:IntArray = intArrayOf(1,2,3) println("x[0]+x[1]=${x[0]+x[1]}") val array4 = IntArray(5) val array5 = IntArray(4){43} val array6 = IntArray(5){it*1} println(array6[4]) //遍历数组的五种方式 //数组遍历 for (item in array1){ println(item) } //带索引遍历数组 for (i in array1.indices){ println("index:$i -> value:${array1[i]}") } //遍历元素 (带索引) for ((index , item) in array1.withIndex()){ println("$index -> $item") } //foreach array1.forEach { println(it) } //foreach 增强版 array1.forEachIndexed { index, i -> println("$index --> $i") } } // Byte Short Int Long Float Double
Dana.Lee
To:Dana_Lee1016@126.com