The approach as @doNotCheckMyBlog mentioned is good, but from what was asked only with the title. Google identifies with this title also n-dimensional arrays, so that I will post here a solution to print n-dimensional arrays for everybody who needs it:
This method is constructed from two functions. One prints the array, and the other one creates an array object from primitives and normal objects. (Workaround of the java.lang.ClassCastException
if you do (Object[])(new int[]{1,2,3})
)
public static String printNdimArray(Object[] in) {
String ret = "[";
for(Object obj : in ) {
if( obj.getClass().isArray() )
ret += printNdimArray( createArrayFromArrayObject(obj) );
else
ret += obj.toString();
ret += ", ";
}
ret = ret.substring(0, ret.length() - 2);
return ret +"]";
}
The other functions casts object or primitive arrays into normal object arrays that can be used to call the above function again, till to the leaves of the original array tree. (Original code from: https://stackoverflow.com/a/6427453)
public static Object[] createArrayFromArrayObject(Object o) {
if(!o.getClass().getComponentType().isPrimitive())
return (Object[])o;
int element_count = Array.getLength(o);
Object elements[] = new Object[element_count];
for(int i = 0; i < element_count; i++)
elements[i] = Array.get(o, i);
return elements;
}
In extraordinary large arrays the structure is clearly not clear anymore, but that can fixed with some tweaks. With a small amount of objects in the array it looks like that:
out: [Mad Max: Fury Road, 7200, [600, 3600, 4000], [EN], [DE, EN, FR], sci_fi]
It also does a great job with displaying 1 dimensional arrays:
out: [FR, EN, DE, CA]