OnJava8-Examples/generics/GenericVarargs.java
2015-05-06 15:14:33 -07:00

25 lines
653 B
Java

//: generics/GenericVarargs.java
import java.util.*;
public class GenericVarargs {
@SuppressWarnings("unchecked")
public static <T> List<T> makeList(T... args) {
List<T> result = new ArrayList<>();
for(T item : args)
result.add(item);
return result;
}
public static void main(String[] args) {
List<String> ls = makeList("A");
System.out.println(ls);
ls = makeList("A", "B", "C");
System.out.println(ls);
ls = makeList("ABCDEFFHIJKLMNOPQRSTUVWXYZ".split(""));
System.out.println(ls);
}
} /* Output:
[A]
[A, B, C]
[, A, B, C, D, E, F, F, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
*///:~