Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[milo-dev] Decoding ArrayDimensions

I was trying to understand the idea behind the unflatten method for the ArrayDimensions that is implemented in milo! Can you give me a small example for what the recursive method is doing in the ArrayUtil.java class?

It is these two methods what I'm trying to understand!

public static Object unflatten(Object array, int[] dimensions) {
Class<?> type = getType(array);

return unflatten(type, array, dimensions, 0);
}

private static Object unflatten(Class<?> type, Object array, int[] dimensions, int offset) {
if (dimensions.length == 1) {
Object a = Array.newInstance(type, dimensions[0]);

for (int i = 0; i < dimensions[0]; i++) {
Array.set(a, i, Array.get(array, offset + i));
}

return a;
} else {
Object a = Array.newInstance(type, dimensions);

int[] tail = Arrays.copyOfRange(dimensions, 1, dimensions.length);

for (int i = 0; i < dimensions[0]; i++) {
Object element = unflatten(type, array, tail, offset + i * length(tail));
Array.set(a, i, element);
}

return a;
}
}

Back to the top