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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 泛型數組 一般來說,數組和泛型并不能很好的結合。你不能實例化參數化類型的數組: ```Java Peel<Banana>[] peels = new Peel<Banana>[10]; // Illegal ``` 類型擦除需要刪除參數類型信息,而且數組必須知道它們所保存的確切類型,以強制保證類型安全。 但是,可以參數化數組本身的類型: ```Java // arrays/ParameterizedArrayType.java class ClassParameter<T> { public T[] f(T[] arg) { return arg; } } class MethodParameter { public static <T> T[] f(T[] arg) { return arg; } } public class ParameterizedArrayType { public static void main(String[] args) { Integer[] ints = { 1, 2, 3, 4, 5 }; Double[] doubles = { 1.1, 2.2, 3.3, 4.4, 5.5 }; Integer[] ints2 = new ClassParameter<Integer>().f(ints); Double[] doubles2 = new ClassParameter<Double>().f(doubles); ints2 = MethodParameter.f(ints); doubles2 = MethodParameter.f(doubles); } } ``` 比起使用參數化類,使用參數化方法很方便。您不必為應用它的每個不同類型都實例化一個帶有參數的類,但是可以使它成為 **靜態** 的。你不能總是選擇使用參數化方法而不用參數化的類,但通常參數化方法是更好的選擇。 你不能創建泛型類型的數組,這種說法并不完全正確。是的,編譯器不會讓你 *實例化* 一個泛型的數組。但是,它將允許您創建對此類數組的引用。例如: ```Java List<String>[] ls; ``` 無可爭議的,這可以通過編譯。盡管不能創建包含泛型的實際數組對象,但是你可以創建一個非泛型的數組并對其進行強制類型轉換: ```Java // arrays/ArrayOfGenerics.java import java.util.*; public class ArrayOfGenerics { @SuppressWarnings("unchecked") public static void main(String[] args) { List<String>[] ls; List[] la = new List[10]; ls = (List<String>[])la; // Unchecked cast ls[0] = new ArrayList<>(); //- ls[1] = new ArrayList<Integer>(); // error: incompatible types: ArrayList<Integer> // cannot be converted to List<String> // ls[1] = new ArrayList<Integer>(); // ^ // The problem: List<String> is a subtype of Object Object[] objects = ls; // So assignment is OK // Compiles and runs without complaint: objects[1] = new ArrayList<>(); // However, if your needs are straightforward it is // possible to create an array of generics, albeit // with an "unchecked cast" warning: List<BerylliumSphere>[] spheres = (List<BerylliumSphere>[])new List[10]; Arrays.setAll(spheres, n -> new ArrayList<>()); } } ``` 一旦你有了對 **List<String>[]** 的引用 , 你會發現多了一些編譯時檢查。問題是數組是協變的,所以 **List<String>[]** 也是一個 **Object[]** ,你可以用這來將 **ArrayList<Integer> ** 分配進你的數組,在編譯或者運行時都不會出錯。 如果你知道你不會進行向上類型轉換,你的需求相對簡單,那么可以創建一個泛型數組,它將提供基本的編譯時類型檢查。然而,一個泛型 **Collection** 實際上是一個比泛型數組更好的選擇。 一般來說,您會發現泛型在類或方法的邊界上是有效的。在內部,擦除常常會使泛型不可使用。所以,就像下面的例子,不能創建泛型類型的數組: ```Java // arrays/ArrayOfGenericType.java public class ArrayOfGenericType<T> { T[] array; // OK @SuppressWarnings("unchecked") public ArrayOfGenericType(int size) { // error: generic array creation: //- array = new T[size]; array = (T[])new Object[size]; // unchecked cast } // error: generic array creation: //- public <U> U[] makeArray() { return new U[10]; } } ``` 擦除再次從中作梗,這個例子試圖創建已經擦除的類型數組,因此它們是未知的類型。你可以創建一個 **對象** 數組,然后對其進行強制類型轉換,但如果沒有 **@SuppressWarnings** 注釋,你將會得到一個 "unchecked" 警告,因為數組實際上不真正支持而且將對類型 **T** 動態檢查 。這就是說,如果我創建了一個 **String[]** , Java將在編譯時和運行時強制執行,我只能在數組中放置字符串對象。然而,如果我創建一個 **Object[]** ,我可以把除了基元類型外的任何東西放入數組。
                  <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>

                              哎呀哎呀视频在线观看