OnJava8-Examples/generics/GenericVarargs.java

31 lines
797 B
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// generics/GenericVarargs.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
import java.util.*;
public class GenericVarargs {
2016-01-25 18:05:55 -08:00
@SafeVarargs
2015-06-15 17:47:35 -07:00
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(""));
2015-06-15 17:47:35 -07:00
System.out.println(ls);
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
[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]
2015-09-07 11:44:36 -06:00
*/