Assignment: Write a generic static method print()
that prints the elements of any object that implements the Iterable<E>
interface.
public class ThisUtil
{
public static <T extends Iterable<T>> void print(T[] anArray) {
ArrayList<String> stringList = new ArrayList<>();
for(T element: anArray)
{
stringList.add(element.toString());
}
String result = String.join(",", stringList);
System.out.println(result);
}
public static void main(String[] args) {
Integer[] list = {1, 2, 5, 9, 3, 7};
ThisUtil.print(list);
}
}
I got error:
no instance(s) of type variable(s) T exist so that Integer conforms to Iterable.
Any help is very appreciated. Thank you advance.