Arrays implement only Serializable and Cloneable in Java 1 ; therefore there is no general way to do this. You will need to implement a separate method for each type of array (since primitive arrays of type int[] cannot be added to Object[] ).
But in this case you do not need it, because Arrays can do it for you:
System.out.println(Arrays.toString(names)); System.out.println(Arrays.toString(numbers));
This will give something like:
[Tom, Dick, Harry]
[1, 2, 3, 4]
If this is not so good, you are stuck to implement a version of your method for every possible array type, for example Arrays .
public static void printAll(Object[] items) { for (Object o : items) System.out.println(o); } public static void printAll(int[] items) { for (int i : items) System.out.println(i); } public static void printAll(double[] items) { for (double d : items) System.out.println(d); }
Please note that this only applies to arrays. Collection implements Iterable , so you can use
public static <T> void printAll(Iterable<T> items) { for (T t : items) System.out.println(t); }
1 See JLS Β§10.7 Array Members .
source share