<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ### for 循環 [TOC] for循環語句是最常用的循環語句,一般用在循環次數已知的情況下。for循環語句的語法格式如下: ``` for (循環對象 in 循環條件) { 執行語句 … } ``` 在上面的語法結構中,for關鍵字后邊()中包括了3部分內容:循環對象、in和循環條件,{}中的執行語句為循環體,in表示在某個范圍內。 示例 ``` fun main(args: Array<String>) { // 循環4次,且步長為1的遞增,0..3表示[0,3] 之間的數字 for (i in 0..3) { //i的值會在0~3之間變化 println("i => $i \t") } } ``` 運行結果 ``` i => 0 i => 1 i => 2 i => 3 ``` 在上述代碼中可以看到,循環條件是“0..3”,這個循環條件的意思是0<=i<=3,當變量i的值在[0,3]這個范圍內時,會執行循環體輸出i的值,變量i的值是從0開始的,每次執行完循環體后變量i的值會自增1(即步長默認為1),直到變量i=4時,不符合條件“0..3”,結束循環。 for 循環可以對任何提供迭代器(iterator)的對象進行遍歷,相當于像 C# 這樣的語言中的`foreach`循環,語法如下: ``` for (item in collection) print(item) for (item in collection) { print(item) } ``` 在使用for循環語句的過程中,如果for循環中只有一條執行語句,則可以去掉循環體的{},簡寫為如下形式 ``` fun main(args: Array<String>) { //循環語句只有一條時,可以進行簡寫 for (i in 0..3) println("i => $i \t") } ``` 循環體可以是一個代碼塊 ``` for (item: Int in ints) { // …… } ``` 如上所述,for可以循環遍歷任何提供了迭代器的對象。即: * 有一個成員函數或者擴展函數`iterator()`,它的返回類型 * 有一個成員函數或者擴展函數`next()`,并且 * 有一個成員函數或者擴展函數`hasNext()`返回`Boolean`。 這三個函數都需要標記為`operator`。 如需在數字區間上迭代,請使用[區間表達式](http://www.kotlincn.net/docs/reference/ranges.html): ``` for (i in 1..3) { println(i) } for (i in 6 downTo 0 step 2) { println(i) } ``` 運行結果 ``` 1 2 3 6 4 2 0 ``` >[info]注意:對區間或者數組的`for`循環會被編譯為并不創建迭代器的基于索引的循環 如果你想要通過索引遍歷一個數組或者一個 list,你可以這么做: ``` for (i in array.indices) { println(array[i]) } ``` 或者你可以用庫函數 withIndex : ``` for ((index, value) in array.withIndex()) { println("the element at $index is $value") } ``` 示例 ~~~ fun main(args: Array<String>) { val items = arrayOf(1, 2, 3, 4, 5, "a") println("--------最基本的遍歷--------") for (item in items) { println(item) } println("--------遍歷的時候,得到索引--------") for (index in items.indices) { println("$index->${items[index]}") } println("--------遍歷的時候,得到索引和值--------") for ((index, value) in items.withIndex()) { println("$index->$value") } } ~~~ 運行結果 ``` --------最基本的遍歷-------- 1 2 3 4 5 a --------遍歷的時候,得到索引-------- 0->1 1->2 2->3 3->4 4->5 5->a --------遍歷的時候,得到索引和值-------- 0->1 1->2 2->3 3->4 4->5 5->a ``` ### 循環嵌套 循環嵌套是指在一個循環語句的循環體中再定義一個循環語句的語法結構,while、do…while、for循環語句都可以進行嵌套,并且它們之間也可以相互嵌套,如最常見的for循環中嵌套for循環,格式如下: ``` for (循環對象in循環條件) { … for (循環對象in循環條件) { 執行語句 … } … } ``` 示例:使用“*”打印直角三角形 ``` fun main(args: Array<String>) { // 循環6次,且步長為1的遞增,0..5表示[0,5]之間 for (i in 0..5) { //外層循環 for (j in 0..i) { //內層循環 print("*") //打印* } print("\n") //換行 } } ``` 運行結果 ``` * ** *** **** ***** ****** ``` 對應Java中的代碼如下 ``` class Test { public static void main(String[] args) { for(int x = 0; x <= 5; x++){ for(int y = 0; y <= x ; y++){ System.out.print("*" ); } System.out.println(); } } } ``` ### forEach循環語句 forEach循環語句,這個循環語句在遍歷數組和集合方面(后面會詳細講解,此處了解即可)為開發人員提供了極大的方便。 #### 普通的forEach語句 普通的forEach語句的格式如下: ``` 調用者.forEach() { println("it=${it}") } ``` 上述語法格式中,調用者可以是數組或集合,it表示數組中的元素。接下來我們通過一段代碼來演示forEach遍歷數組中的元素 示例 ``` fun main(args: Array<String>) { var arr: IntArray = intArrayOf(1, 2, 3, 4) //定義一個數組arr并初始化該數組 arr.forEach() { print(it.toString() + "\t") //it表示數組中對應的元素對象 } } ``` 運行結果 ``` 1 2 3 4 ``` #### 帶角標的forEachIndexed語句 帶角標的forEachIndexed語句的格式如下: ``` 調用者.forEachIndexed() { index, it -> println("角標=$index元素=${it}") } ``` 上述語法格式中,index表示數組角標或者是集合的索引,it表示數組角標或者是集合索引中對應的元素。接下來我們通過一段代碼來演示forEachIndexed遍歷數組中的元素以及角標 ``` fun main(args: Array<String>) { var arr: IntArray = intArrayOf(1, 2, 3, 4) //定義一個數組arr并初始化該數組 arr.forEachIndexed() { index, it -> println("角標=$index 元素=$it") } } ``` 運行結果 ``` 角標=0 元素=1 角標=1 元素=2 角標=2 元素=3 角標=3 元素=4 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看