OnJava8-Examples/generics/ArrayMaker.java

22 lines
640 B
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: generics/ArrayMaker.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
import java.lang.reflect.*;
import java.util.*;
public class ArrayMaker<T> {
private Class<T> kind;
public ArrayMaker(Class<T> kind) { this.kind = kind; }
@SuppressWarnings("unchecked")
T[] create(int size) {
return (T[])Array.newInstance(kind, size);
}
public static void main(String[] args) {
ArrayMaker<String> stringMaker =
2015-05-05 11:20:13 -07:00
new ArrayMaker<>(String.class);
2015-04-20 15:36:01 -07:00
String[] stringArray = stringMaker.create(9);
System.out.println(Arrays.toString(stringArray));
}
} /* Output:
[null, null, null, null, null, null, null, null, null]
*///:~