i want to make array with using generices.
but compile error occurred.
How to make array with using generices?
public class Test {
public static void main(String[] args) {
String[] datas = Test.changeToArray("apple");
for (String data : datas) {
System.out.println(data);
}
Boolean[] bools = Test.changeToArray(true);
for (Boolean bool : bools) {
System.out.println(bool);
}
}
public static <T> T[] changeToArray(T data) {
// i want this..
// but this is compile error
// Cannot create a generic array of T
T[] datas = new T[] {data};
return datas;
}
}