<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Kotlin 程序:按字典順序對元素進行排序 > 原文: [https://www.programiz.com/kotlin-programming/examples/lexicographical-order-words](https://www.programiz.com/kotlin-programming/examples/lexicographical-order-words) #### 在此程序中,您將學習使用`for`循環和 Kotlin 中的`if`循環按字典順序對元素詞進行排序。 ## 示例:按字典順序對字符串排序的程序 ```kt fun main(args: Array<String>) { val words = arrayOf("Ruby", "C", "Python", "Java") for (i in 0..2) { for (j in i + 1..3) { if (words[i].compareTo(words[j]) > 0) { // swap words[i] with words[j[ val temp = words[i] words[i] = words[j] words[j] = temp } } } println("In lexicographical order:") for (i in 0..3) { println(words[i]) } } ``` 運行該程序時,輸出為: ```kt In lexicographical order: C Java Python Ruby ``` 在上述程序中,要排序的 5 個單詞的列表存儲在變量`word`中。 然后,我們遍歷每個單詞(`words[i]`),并將其與數組中之后的所有單詞(`words[j]`)進行比較。 這是通過使用字符串的`compareTo()`方法完成的。 如果`compareTo()`的返回值大于 0,則必須在位置上進行交換,即`word[i]`在`word[j]`之后。 因此,在每次迭代中,`words[i]`包含最早的單詞。 執行步驟 | 迭代 | 初始單詞 | `i` | `j` | `words[]` | | --- | --- | --- | --- | --- | | 1 | `{ "Ruby", "C", "Python", "Java" }` | 0 | 1 | `{ "C",?"Ruby", "Python", "Java" }` | | 2 | `{ "C",?"Ruby", "Python", "Java" }` | 0 | 2 | `{ "C",?"Ruby", "Python", "Java" }` | | 3 | `{ "C",?"Ruby", "Python", "Java" }` | 0 | 3 | `{ "C",?"Ruby", "Python", "Java" }` | | 4 | `{ "C",?"Ruby", "Python", "Java" }` | 1 | 2 | `{ "C", "Python",?"Ruby", "Java" }` | | 5 | `{ "C", "Python",?"Ruby", "Java" }` | 1 | 3 | `{ "C", "Java",?"Ruby", "Python" }` | | 最后 | `{ "C", "Java",?"Ruby", "Python" }` | 2 | 3 | `{ "C", "Java", "Python",?"Ruby" }` | 以下是等效的 Java 代碼:[以字典順序對單詞進行排序的 Java 程序](/java-programming/examples/lexicographical-order-words "Java program to sort words in lexicographical order")
                  <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>

                              哎呀哎呀视频在线观看