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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 如何獲取`ArrayList`的子列表 > 原文: [https://beginnersbook.com/2013/12/how-to-get-sublist-of-an-arraylist-with-example/](https://beginnersbook.com/2013/12/how-to-get-sublist-of-an-arraylist-with-example/) 在本教程中,我們將了解如何從現有`ArrayList`獲取子列表。我們將使用`ArrayList`類的[`subList`](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#subList(int, int))方法來完成它。 `List subList(int fromIndex, int toIndex)` 這里`fromIndex`是包容性的,`toIndex`是獨占的。關于這種方法,我在本文末尾分享了一些重要的觀點。 ## 從`ArrayList`獲取子列表的示例 以下示例中的注意事項: `subList`方法返回一個列表,因此將子列表存儲在另一個`ArrayList`中,我們必須以與下面示例中相同的方式對返回值進行類型轉換。另一方面,如果我們將返回的子列表存儲到列表中,則無需鍵入轉換(請參閱示例)。 ```java package beginnersbook.com; import java.util.ArrayList; import java.util.List; public class SublistExample { public static void main(String a[]){ ArrayList<String> al = new ArrayList<String>(); //Addition of elements in ArrayList al.add("Steve"); al.add("Justin"); al.add("Ajeet"); al.add("John"); al.add("Arnold"); al.add("Chaitanya"); System.out.println("Original ArrayList Content: "+al); //Sublist to ArrayList ArrayList<String> al2 = new ArrayList<String>(al.subList(1, 4)); System.out.println("SubList stored in ArrayList: "+al2); //Sublist to List List<String> list = al.subList(1, 4); System.out.println("SubList stored in List: "+list); } } ``` 輸出: ```java Original ArrayList Content: [Steve, Justin, Ajeet, John, Arnold, Chaitanya] SubList stored in ArrayList: [Justin, Ajeet, John] SubList stored in List: [Justin, Ajeet, John] ``` **注意:** 如果指定的索引超出了`ArrayList`的范圍(`fromIndex > 0 || toIndex < size`),則`subList`方法拋出[`IndexOutOfBoundsException`](https://docs.oracle.com/javase/7/docs/api/java/lang/IndexOutOfBoundsException.html)。 [`IllegalArgumentException`](https://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.htm) - 如果起始索引大于終點索引(`fromIndex < toIndex`)。
                  <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>

                              哎呀哎呀视频在线观看