<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之旅 廣告
                ## [添加元素組](https://lingcoder.gitee.io/onjava8/#/book/12-Collections?id=%e6%b7%bb%e5%8a%a0%e5%85%83%e7%b4%a0%e7%bb%84) 在**java.util**包中的**Arrays**和**Collections**類中都有很多實用的方法,可以在一個**Collection**中添加一組元素。 `Arrays.asList()`方法接受一個數組或是逗號分隔的元素列表(使用可變參數),并將其轉換為**List**對象。`Collections.addAll()`方法接受一個**Collection**對象,以及一個數組或是一個逗號分隔的列表,將其中元素添加到**Collection**中。下邊的示例展示了這兩個方法,以及更通用的 、所有**Collection**類型都包含的`addAll()`方法: ~~~ // collections/AddingGroups.java // Adding groups of elements to Collection objects import java.util.*; public class AddingGroups { public static void main(String[] args) { Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); Integer[] moreInts = { 6, 7, 8, 9, 10 }; collection.addAll(Arrays.asList(moreInts)); // Runs significantly faster, but you can't // construct a Collection this way: Collections.addAll(collection, 11, 12, 13, 14, 15); Collections.addAll(collection, moreInts); // Produces a list "backed by" an array: List<Integer> list = Arrays.asList(16,17,18,19,20); list.set(1, 99); // OK -- modify an element // list.add(21); // Runtime error; the underlying // array cannot be resized. } } ~~~ **Collection**的構造器可以接受另一個**Collection**,用它來將自身初始化。因此,可以使用`Arrays.asList()`來為這個構造器產生輸入。但是,`Collections.addAll()`運行得更快,而且很容易構建一個不包含元素的**Collection**,然后調用`Collections.addAll()`,因此這是首選方式。 `Collection.addAll()`方法只能接受另一個**Collection**作為參數,因此它沒有`Arrays.asList()`或`Collections.addAll()`靈活。這兩個方法都使用可變參數列表。 也可以直接使用`Arrays.asList()`的輸出作為一個**List**,但是這里的底層實現是數組,沒法調整大小。如果嘗試在這個**List**上調用`add()`或`remove()`,由于這兩個方法會嘗試修改數組大小,所以會在運行時得到“Unsupported Operation(不支持的操作)”錯誤: ~~~ // collections/AsListInference.java import java.util.*; class Snow {} class Powder extends Snow {} class Light extends Powder {} class Heavy extends Powder {} class Crusty extends Snow {} class Slush extends Snow {} public class AsListInference { public static void main(String[] args) { List<Snow> snow1 = Arrays.asList( new Crusty(), new Slush(), new Powder()); //- snow1.add(new Heavy()); // Exception List<Snow> snow2 = Arrays.asList( new Light(), new Heavy()); //- snow2.add(new Slush()); // Exception List<Snow> snow3 = new ArrayList<>(); Collections.addAll(snow3, new Light(), new Heavy(), new Powder()); snow3.add(new Crusty()); // Hint with explicit type argument specification: List<Snow> snow4 = Arrays.<Snow>asList( new Light(), new Heavy(), new Slush()); //- snow4.add(new Powder()); // Exception } } ~~~ 在**snow4**中,注意`Arrays.asList()`中間的“暗示”(即`<Snow>`),告訴編譯器`Arrays.asList()`生成的結果**List**類型的實際目標類型是什么。這稱為*顯式類型參數說明*(explicit type argument specification)。
                  <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>

                              哎呀哎呀视频在线观看