Arays.asList着信パラメータの問題

3585 ワード

  • は、列挙によってprmitiveおよび参照タイプが導入されると、各パラメータは、Listの要素
  • として機能する。
  • が行列に入ったとき、参照型の配列であれば、配列の各要素は、Listの要素として機能する。prmitiveタイプの配列であれば、問題が発生します。この配列はListの要素として扱われます。

  • 次の例を見ます
    Integer[] arrInteger = new Integer[10];
    arrInteger[0] = 1;
    arrInteger[1] = 2;
    
    //         
    System.out.println(Arrays.asList(arrInteger).get(0));
    
    //       
    System.out.println(Arrays.asList(1,2,3).get(0));
    
    int[] arrInt = new int[10];
    arrInt[0] = 1;
    arrInt[1] = 2;
    
    // int[]     :   
    System.out.println(Arrays.asList(arrInt).get(0));
    
    // int/char/short/double/boolean...     
    出力:
    1
    1
    [I@d542094
    問題はjavaの可変パラメータタイプに出ています。Arays.asList(T...arr)
    なぜ可変パラメータタイプがprmitiveタイプの配列に入ると単一の要素になるのですか?
    T...arrの声明はT[]arrの声明に相当し、コンパイラはcalerが入ってきたところで1つの配列を初期化し、すべての変化を行列に参加し、その後にcaleeを着用する。
    范型は参照タイプだけに対して、范型Tはextensを通じて親タイプを指定しないと、デフォルトはObjectタイプであるので、この方法のバイトコード署名は、実は:([Ljava/lang/Object;)Ljava/util/List;prmitiveタイプが列挙されるように導入されると、Arrays.asList(1, 2, 3)などのint型パラメータが参照タイプではないため、コンパイラがatoboxingを作成し、Integerタイプの配列を生成してasListに入るようにトリガされる。
    int型配列が入ってくると、配列は既に引用タイプ(タイプは「I」)であり、autbox ingは再び作用する必要がなく、直接に配列をObjectとしてasList方法に伝えます。
    Java Autobox ingのトリガタイミング
    Coverting a prmitive value(an int,for example)into an object of the cores ponding wrapper class(Integer)is caled toxbox ing.The Java compler atomitive value:
  • Passed as a parameter to a method expects an object of the cores ponding wrapper class.
  • Asigned to a variable of the cores ponding wrapper class.
  • Auto unboxingのトリガタイミングCoverting an Object of a wrapper type(Integer)to its coreponding prmitive(int)value is caled unbox ing.The Java compler appler unbox ing when n object of a wrapper class:
  • Passed as a parameter to a method expects a value of the cores ponding prmitive type.
  • Asigned to a variable of the cores ponding prmitive type.https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
  • 拡張知識:prmitive型配列と引用型配列の違いは、どのような参照型の配列もObject[]型変数にコピーできますが、prmitive型配列はできません。
    Object[] a = new Integer[2];  //  warning,     
    Object[] a = new int[2];      //