Skip to content

Instantly share code, notes, and snippets.

@ssullivan
Last active April 26, 2020 19:13
Show Gist options
  • Select an option

  • Save ssullivan/a17e823923d89205db59ccdebcc443da to your computer and use it in GitHub Desktop.

Select an option

Save ssullivan/a17e823923d89205db59ccdebcc443da to your computer and use it in GitHub Desktop.
Recurse through multi dimensional arrays
// these probably need some input checks
// recursive
private static Object getValue(Object baseArray, int dim, int[] coordinates) {
if (dim + 1 >= coordinates.length) {
return Array.get(baseArray, coordinates[dim]);
}
else {
return getValue(Array.get(baseArray, coordinates[dim]), dim + 1, coordinates);
}
}
// iterative
private static Object getValue0(Object baseArray, int dim, int[] coordinates) {
Object array = baseArray;
for (int i = dim; i < coordinates.length; ++i) {
array = Array.get(array, coordinates[dim]);
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment