OnJava8-Examples/generics/GenericVarargs.java

26 lines
694 B
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: generics/GenericVarargs.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.util.*;
public class GenericVarargs {
2015-05-06 15:14:33 -07:00
@SuppressWarnings("unchecked")
2015-04-20 15:36:01 -07:00
public static <T> List<T> makeList(T... args) {
2015-05-05 11:20:13 -07:00
List<T> result = new ArrayList<>();
2015-04-20 15:36:01 -07:00
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]
*///:~