遍历字符串
此用法在数据类型章节中的字符串类型顶用到过。还不甚清跋扈的可以查看 Kotlin——最具体的数据类型介绍。
例:
- for (i in "abcdefg"){
- print("i => $i \t")
- }
输出结不雅为:
遍历数组
关键字:downTo
例:
- var arrayListTwo = arrayOf(1,3,5,7,9)
- for (i in arrayListTwo.indices){
- println("arrayListTwo[$i] => " + arrayListTwo[i])
- }
- var arrayListOne = arrayOf(10,20,30,40,50)
- for (i in arrayListOne){
- print("i => $i \t")
- }
输出结不雅为:
- i => 10 i => 20 i => 30 i => 40 i => 50
应用数组的indices属性遍历
例:
输出结不雅为:
- val l: Int = if (b != null) b.length else -1
打印:
- arrayListTwo[0] => 1
- arrayListTwo[1] => 3
- arrayListTwo[2] => 5
- arrayListTwo[3] => 7
- arrayListTwo[4] => 9
应用数组的withIndex()办法遍历
例:
- var arrayListTwo = arrayOf(1,3,5,7,9)
- for ((index,value) in arrayListTwo.withIndex()){
- println("index => $index \t value => $value")
- }
输出结不雅为:
- index => 0 value => 1
- index => 1 value => 3
- index => 2 value => 5
- index => 3 value => 7
- index => 4 value => 9
应用列表或数组的扩大函数遍历
数组或列表有一个成员或扩大函数iterator()实现了Iterator接口,且该接口供给了next()与hasNext()两个成员或扩大函数
其一般和while轮回一路应用
- public interface Iterator<out T> {
- /**
- * Returns the next element in the iteration.
- */
推荐阅读
沙龙晃荡 | 3月31日 京东、微博拭魅战专家与你合营商量容器技巧实践! 在很多行业中都有很多看起来竽暌剐事理,用起往来交往不是那回事的误区,在主机中更是有不少,受愚钱是一方面,硬件和>>>详细阅读
本文标题:不要用Java的语法思维来写Kotlin
地址:http://www.17bianji.com/lsqh/40601.html
1/2 1