added generics example method and tests

This commit is contained in:
Yasser Afifi
2016-11-20 20:20:52 +00:00
parent 5f99e5aedd
commit 6835625602
2 changed files with 70 additions and 0 deletions
@@ -0,0 +1,24 @@
import java.util.ArrayList;
import java.util.List;
public class Generics {
// definition of a generic method
public static <T> List<T> fromArrayToList(T[] a) {
List<T> list = new ArrayList<>();
for (T t : a) {
list.add(t);
}
return list;
}
// example of a generic method that has Number as an upper bound for T
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
List<T> list = new ArrayList<>();
for (T t : a) {
list.add(t);
}
return list;
}
}