How to access a multidimensional array programmatically in Java?

Suppose we have a multidimensional array, and the number of measurements is known only at runtime. And suppose we have an integer number of indices.

How to apply indexes to an array to access an array element?

UPDATE

Let's pretend that:

int [] indices = new int { 2, 7, 3, ... , 4}; // indices of some element int X = indices.length; // number of dimensions Object array = .... // multidimensional array with number of dimensions X ... 

I want to get the element addressed by indices indices from array .

UPDATE 2

I wrote the following recursion based code:

 package tests; import java.util.Arrays; public class Try_Multidimensional { private static int element; public static int[] tail(int[] indices) { return Arrays.copyOfRange(indices, 1, indices.length); } public static Object[] createArray(int ... sizes) { Object[] ans = new Object[sizes[0]]; if( sizes.length == 1 ) { for(int i=0; i<ans.length; ++i ) { ans[i] = element++; } } else { for(int i=0; i<ans.length; ++i) { ans[i] = createArray(tail(sizes)); } } return ans; } public static Object accessElement(Object object, int ... indices) { if( object instanceof Object[] ) { Object[] array = (Object[]) object; return accessElement(array[indices[0]], tail(indices)); } else { return object; } } public static void main(String[] args) { element = 0; Object array = createArray(4, 5, 12, 7); System.out.println(accessElement(array, 0, 0, 0, 0)); System.out.println(accessElement(array, 0, 0, 0, 1)); System.out.println(accessElement(array, 1, 0, 10, 0)); try { System.out.println(accessElement(array, 0, 5, 0, 1)); } catch(Exception e) { System.out.println(e.toString()); } System.out.println(4*5*12*7-1); System.out.println(accessElement(array, 3, 4, 11, 6)); } } 

Questions:

1) Are there reliable ready-made methods from the JDK and / or well-known libraries for this?

2) I used Object . can it be avoided? can I create / access an array of variable dimension of built-in or specific type? how big is the gain due to using Object ?

+6
source share
4 answers
 int index(Object arrayToIndex, int... indices) { for (int i = 0; i < indices.length - 1; i++) { arrayToIndex = ((Object[]) arrayToIndex)[indices[i]]; } return ((int[]) arrayToIndex)[indices[indices.length-1]]; } 

Scroll through the dimensions and index each dimension one at a time. Jobs and a special case for the last dimension will be annoying, so I recommend wrapping it in some sort of n-dimensional array class. ( It looks like some options already exist. )

+2
source

You can define the size of each dimension as separate arrays (because that's what they are):

 public void someMEthod(int[][][] matrix) { int d1 = matrix.length; int d2 = 0; int d3 = 0; if(d1 > 0) { d2 = matrix[0].length; if(d2 > 0) { d3 = matrix[0][0].length; } } System.out.println("Dimension 1 is " + d1); System.out.println("Dimension 2 is " + d2); System.out.println("Dimension 3 is " + d3); } 

I hope this helps.

0
source

I found a curious way to do this using reflection. This is just the code I put together, but you can wrap it in a class and make it all beautiful.

 // build and fill an array to the given depth public static Object[] constructArray(Object[] array, int depth) { if(depth == 0) return null; for(int i=0;i<array.length;i++) { Array.set(array, i, constructArray(new Object[array.length], depth-1)); } return array; } // sets a value in the multi dimensional array using the indicies public static void setArrayUsingIndecies(Object array, int[] indicies, Object value) { if(indicies.length == 0) return; for(int i=0;i<indicies.length-1;i++) { array = Array.get(array, indicies[i]); } Array.set(array, indicies[indicies.length-1], value); } // gets a value in the multi dimmensional array using the indicies public static Object getArrayUsingIndecies(Object array, int[] indicies) { Object value = array; for(int i=0;i<indicies.length;i++) { value = Array.get(value, indicies[i]); } return value; } 

Here are some sample code

 int numberOfDimmensions = 2; Object array = constructArray(new Object[numberOfDimmensions], numberOfDimmensions); int [] indices = new int [] { 0, 1 }; setArrayUsingIndecies(array, indices, "Hello"); System.out.println(getArrayUsingIndecies(array, indices)); // Hello indices = new int [] { 0, 0 }; System.out.println(getArrayUsingIndecies(array, indices)); // null 
0
source

Is it easier than we think? How about this approach:

 int [] indices = new int { 2, 7, 3, ... , 4}; // indices of some element int X = indices.length; // number of dimensions Object array = new Object[X].... // multidimensional array with number of dimensions X 

and then:

 Object myObject = array[indices[1]] // myObject references the 7th element of array 

However, you must make sure that your index array does not contain a number larger than the index size - 1. For example

 indices = new int [5,4,3,2,1] // ok indices = new int [6,4,3,2,1] // not ok, because you would access the 6th Element in an arry with length 5 
0
source

Source: https://habr.com/ru/post/971412/


All Articles