I try to write a generic method to print:
- A list of element such as list of Integer, String, etc.
- A list of T[] such as
List<String[]>, List<Integer[]>
etc.
I have following two methods:
public static <T> void printList(List<T> list){
for(T n : list) {
System.out.print("[" + n + "]");
}
}
public static <T> void printList(List<T[]> list){
for(T[] arr: list) {
for(T n : arr){
System.out.print("[" + n + "]");
}
}
}
I got compile error:
public static <T> void printList(List<T[]> list){
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>printList(List<T#1[]>)
T#2 extends Object declared in method <T#2>printList(List<T#2>)
Is there any way I can have a method printList for List<T>
and List<T[]>
?