<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 如何交換`ArrayList`中的兩個元素 > 原文: [https://beginnersbook.com/2013/12/how-to-swap-two-elements-in-an-arraylist/](https://beginnersbook.com/2013/12/how-to-swap-two-elements-in-an-arraylist/) 本教程將幫助您了解如何在[`ArrayList`](https://beginnersbook.com/2013/12/java-arraylist/)中交換兩個元素。我們使用[`Collections.swap()`](https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#swap(java.util.List, int, int))方法進行交換。 `public static void swap(List list, int i1, int i2)` 該方法將索引`i1`的元素與索引`i2`的元素交換。它拋出`IndexOutOfBoundsException` - 如果`i1`或`i2`小于零或大于列表的大小(`i1 < 0 || i1 >= list.size() || i2 < 0 || i2 >= list.size()`??)。 ## 在`ArrayList`中交換兩個元素的示例 在這個例子中,我們有一個`ArrayList<String>`我們使用`Collections.swap()`方法交換`ArrayList`的第二個(`index = 1`)和第五個(`index = 4`)元素。 ```java package beginnersbook.com; import java.util.ArrayList; import java.util.Collections; public class SwappingExample { public static void main(String a[]){ ArrayList<String> al = new ArrayList<String>(); al.add("Sachin"); al.add("Rahul"); al.add("Saurav"); al.add("Sunil"); al.add("Kapil"); al.add("Vinod"); System.out.println("ArrayList before Swap:"); for(String temp: al){ System.out.println(temp); } //Swapping 2nd(index 1) element with the 5th(index 4) element Collections.swap(al, 1, 4); System.out.println("ArrayList after swap:"); for(String temp: al){ System.out.println(temp); } } } ``` 輸出: ```java ArrayList before Swap: Sachin Rahul Saurav Sunil Kapil Vinod ArrayList after swap: Sachin Kapil Saurav Sunil Rahul Vinod ```
                  <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>

                              哎呀哎呀视频在线观看