Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstracting dimensionality of arrays in Java

In Java, arrays of different dimensionalities have different types. So a method that takes int[] as a parameter cannot take int[][] or int[][][]. I have a lot of code where I create methods that are quite similar but for the dimensionality of the array. Is there a way to handle arrays of arbitrary dimensionality, and thus abstract out this common functionality?

like image 293
Matthew Piziak Avatar asked May 27 '12 03:05

Matthew Piziak


2 Answers

If you are willing to forego type safety, you can do it with a little recursion (no surprise here, right?) and reflection.

The idea is to write your method in a way that it recurses down until the array has only one dimension. Once you're at the single-dimension level, do the work; otherwise, call yourself recursively, and aggregate your findings from the prior levels if necessary.

Here is a quick demo:

import java.util.*;
import java.lang.*;
import java.lang.reflect.Array;

class Main {
    public static int sumArray(Object array) {
            Class type = array.getClass();
            if (!type.isArray()) {
                    throw new IllegalArgumentException("array");
            }
            Class ct = type.getComponentType();
            int res = 0;
            int len = Array.getLength(array);
            if (ct.isArray()) {
                    for (int i = 0 ; i != len ; i++) {
                            res += sumArray(Array.get(array, i));
                    }
            } else {
                    for (int i = 0 ; i != len ; i++) {
                            res += Array.getInt(array, i);
                    }
            }
            return res;
    }
    public static void main (String[] args) throws java.lang.Exception
    {
            int[] a = new int[] {1,2,3,4,5,6,7,8,9,10};
            int aa[][] = new int[][] {{1,2},{3,4},{5,6}};
            int aaa[][][] = new int[][][]{{{1,2},{3,4},{5,6}},{{7,8},{9,10},{11,12}}};
            System.out.println(sumArray(a));
            System.out.println(sumArray(aa));
            System.out.println(sumArray(aaa));
    }
}
like image 173
Sergey Kalinichenko Avatar answered Sep 28 '22 05:09

Sergey Kalinichenko


I'd say the closest thing you can do is make a few overload methods that take all the available dimensions of array and have each of them call a common method that has much of the logic.

public void method ( int [] i ) {  //1-d array
    //logic 
}

public void method ( int [][] i) {  //2-d array
     for (int [] j : i) {
         method(j);
     }
}

public void method ( int [][][] i) {   // 3-d array
    for ( int [][] j : i ) {
        method(j);
    }
}

...etc

like image 34
Kevin DiTraglia Avatar answered Sep 28 '22 05:09

Kevin DiTraglia