How to cast a multidimensional array without knowing the dimension in Java

If you want to work with an array which dimension is not known at the compile time, I would suggest you to recursively process all of its entries instead of trying to cast it.

You can use object.getClass().isArray() method to check if the current entry is an array and then iterate over it using Array.getLength(object) and Array.get(object, i):

public static void main(String[] args) {
    Object array = new String[][] {new String[] {"a", "b"}, new String[] {"c", "d"}};
    processArray(array, System.out::println);
}

public static void processArray(Object object, Consumer<Object> processor) {
    if (object != null && object.getClass().isArray()) {
        int length = Array.getLength(object);
        for (int i = 0; i < length; i ++) {
            Object arrayElement = Array.get(object, i);
            processArray(arrayElement, processor);
        }
    } else {
        processor.accept(object);
    }
}