基本型态阵列
Kotlin 已经有内建一些阵列物件,如 ByteArray、IntArray、DoubleArray 等。它们正好是存放基本资料型态的元素,宣告时也有对应的建构式可使用。
// 仅仅只是宣告,尚未赋予元素值
val scores = IntArray(5)
// 元素值是已知的情况
// 用「Of」结尾的方法宣告,代表可以同时给予元素值
val scores = intArrayOf(68, 90, 76, 88, 92)
存取阵列元素
val names = arrayOf("Anna", "Bonnie", "Cynthia", "Darcy", "Edison")
val scores = IntArray(4)
scores[0] = 10
scores[1] = 20
scores[2] = 30
scores[3] = 40
// 将元素逐一取出,赋值给临时变数
for (score in scores) {
println(score)
}
// 由阵列提供索引范围
// 阵列的 indices 方法会回传索引的范围,其顺序为 0 到 阵列容量-1
for (i in scores.indices) {
println(scores[i])
}
// size建立范围
for (i in 0 until scores.size) {
printn(scores[i])
}
// withIndex 方法会回传多个 IndexedValue 物件,包含索引与元素两项资料,与 key-value 的概念类似
for ((i, value) in names.withIndex()) {
println("${i + 1}. ${value.toUpperCase()}")
}
谢谢大家愿意花时间阅读,小弟弟我在此鞠躬
<<: Day 25: Mac M1 萤幕录制 feat. GCP Lab demo
>>: [Day 25] 阿嬷都看得懂的 CSS 微互动元件
“Slept, awoke, slept, awoke, miserable life.” ― f...
前言 相对於数据视觉化操作,官方在外观调整的教学算是较少,或许难下关键字也是一个问题。平常在实作的时...
前天使用 updown.io 架设了 status page,并且让它可以在服务无法连上的时候,自动...
Display 我认为有了html和css的基本概念後 还有一个观念需要知道的 那就是display...
上一篇我们说到怎样才算是把菜单送到老板手上呢? 送出菜单有两个步骤 顾客送出菜单 老板接收菜单 我们...