Home > Java > javaTutorial > body text

java—Constructing an array with generic parameter types

黄舟
Release: 2017-03-01 11:44:04
Original
1955 people have browsed it


I suddenly thought of a question when I was coding a while ago. Usually our array is passed into the method as a parameter. What if we want to create an array in the method? When the type is clear, this is not difficult. What if the parameters we pass in are parameters of a generic type?

public static <T> T[]  creArray (T obj){
        T[] arr = new T[10];
}
Copy after login
Copy after login

The above method of using T to create a new array directly is wrong, and an error will appear during compilation: Cannot create a generic array of T.. Java does not support directly creating arrays of unknown types.

Finally I got such a perfect solution:

package Test;import java.lang.reflect.Array;/**
 * 
 * @author QuinnNorris
 * 在泛型方法中创建泛型类型的数组
 */public class Test {
    public static void main(String[] args) {        // TODO Auto-generated method stub

        String a = "ccc";//创建一个String,作为泛型类型
        String[] ar = creArray(a);        for(String art :ar)//循环打印
            System.out.println(art);
    }    //泛型静态方法
    public static <T> T[]  creArray (T obj){
        T[] arr = (T[])Array.newInstance(obj.getClass(), 5);
        arr[1] = obj;
        System.out.println(arr[1]);        return arr;
    }
}
Copy after login
Copy after login

The code output is as follows:

ccc    //方法中输出的arr[1] 

  null    //以下5个是main中循环迭代出的数组值 

  ccc 

  null 

  null 

  null
Copy after login
Copy after login


The above method is completely feasible. We construct an array of a specifiable type by using the newInstance method of the Array class. It should also make sense to use reflection to do this job. Because the generic type T can only be determined at runtime, we must find a way to create a generic array at Java runtime. The only technology that works at Java runtime is reflection.

I also saw null, so I would like to sort out the values ​​of different types of array initialization in Java:

  1. Basic type (numeric type): 0

  2. Basic type (boolean): false

  3. Basic type (char type): (char) 0

  4. Object type: null

I suddenly thought of a problem when I was coding a while ago. Usually our array is passed into the method as a parameter. What if we want to create an array in the method? When the type is clear, this is not difficult. What if the parameters we pass in are parameters of a generic type?

public static <T> T[]  creArray (T obj){
        T[] arr = new T[10];
}
Copy after login
Copy after login

The above method of using T to directly create a new array is wrong, and an error will appear during compilation: Cannot create a generic array of T.. Java does not support directly creating arrays of unknown types.

Finally I got such a perfect solution:

package Test;import java.lang.reflect.Array;/**
 * 
 * @author QuinnNorris
 * 在泛型方法中创建泛型类型的数组
 */public class Test {
    public static void main(String[] args) {        // TODO Auto-generated method stub

        String a = "ccc";//创建一个String,作为泛型类型
        String[] ar = creArray(a);        for(String art :ar)//循环打印
            System.out.println(art);
    }    //泛型静态方法
    public static <T> T[]  creArray (T obj){
        T[] arr = (T[])Array.newInstance(obj.getClass(), 5);
        arr[1] = obj;
        System.out.println(arr[1]);        return arr;
    }
}
Copy after login
Copy after login

The code output is as follows:

ccc    //方法中输出的arr[1] 

  null    //以下5个是main中循环迭代出的数组值 

  ccc 

  null 

  null 

  null
Copy after login
Copy after login


The above method is completely feasible. We construct an array of a specifiable type by using the newInstance method of the Array class. It should also make sense to use reflection to do this job. Because the generic type T can only be determined at runtime, we must find a way to create a generic array at Java runtime. The only technology that can work at Java runtime is reflection.

I also saw null, so I would like to sort out the values ​​of different types of array initialization in Java:

  1. Basic type (numeric type): 0

  2. Basic type (boolean): false

  3. Basic type (char type): (char) 0

  4. Object type: nul

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!