Last active
April 26, 2020 19:13
-
-
Save ssullivan/a17e823923d89205db59ccdebcc443da to your computer and use it in GitHub Desktop.
Recurse through multi dimensional arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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