Adding two n-dimensional arrays in Java
addVectors
Two 1-dimensional arrays / vectors in Java can be added like this:
public static int[] addVectors( int[] a, int[] b )
{
int[] c = new int[a.length];
for ( int i = 0; i < a.length; i++ )
{
c[i] = a[i] + b[i];
}
return c;
}
addMatrices
Two 2-dimensional arrays / matrices in Java can be added like this:
public static int[][] addMatrices( int[][] a, int[][] b )
{
int[][] c = new int[a.length][a[0].length];
for ( int i = 0; i < a.length; i++ )
{
c[i] = addVectors( a[i], b[i] );
}
return c;
}
Both functions require the arrays to be of the same size to avoid an arrayOutOfBoundsException
.
addArraysN
There should be a way to add two arrays of unknown dimension using recursion.
For example, the following code using the hypothetical function addArraysN( arr1, arr2 )
int[][][] a = {
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } },
{ { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } }
};
int[][][] b = {
{ { 2, 2, 2 }, { 2, 2, 2 }, { 2, 2, 2 } },
{ { 2, 2, 2 }, { 2, 2, 2 }, { 2, 2, 2 } },
{ { 2, 2, 2 }, { 2, 2, 2 }, { 2, 2, 2 } }
};
int[][][] sum = addArraysN( a, b );
System.out.println( java.util.Arrays.deepToString( sum ) );
should output
[[[3, 3, 3], [3, 3, 3], [3, 3, 3]], [[3, 3, 3], [3, 3, 3], [3, 3, 3]], [[3, 3, 3], [3, 3, 3], [3, 3, 3]]]
Now I'm wondering about how to implement this function addArraysN( arr1, arr2 )
.
I started out with the following pseudo code:
addArraysN( arr1, arr2 )
{
int dimension = getDimension( arr1 );
if ( dimension == 0 ) //there are no arrays, only numbers
return arr1 + arr2;
else
{
//create a new arrays with the same dimension and size as arr1 / arr2
//loop through the fields with for
//call itself when adding the fields of arr1 and arr2
//return the sum
}
}
New arrays can be created using the newInstance
-method from java.lang.reflect.Array.
Looping can be made possible like this:
for ( int i = 0; i < ((int[])arr1).length; i++ )
sum = addArraysN( ((int[])arr1)[i], ((int[])arr2)[i] );
Question
But I stumbled over a lot of runtime errors and other problems. Has anybody an idea or even a solution on how to implement this addArrayN
-method?
It should be also possible to work with ArrayList
or any other Class but I'm mainly interested on how to do this with arrays... (Nevertheless if someone knows it please post!)
Thanks in advance
appendix 1
My original code:
import java.util.Arrays;
import java.lang.reflect.Array;
public class ArrayN
{
public static void main( String[] args )
{
//Vector
int[] vector1 = {0, 1, 2, 3, 4};
int[] vector2 = {4, 3, 2, 1, 0};
int[] vector3 = ArrayN.addVectors( vector1, vector2 );
for ( int num : vector3 )
{
System.out.print( num );
}
System.out.println();
System.out.println();
//Matrix
int[][] matrix1 = {{0, 1, 2, 3, 4}, {4, 3, 2, 1, 0}};
int[][] matrix2 = {{4, 3, 2, 1, 0}, {0, 1, 2, 3, 4}};
int[][] matrix3 = ArrayN.addMatrices( matrix1, matrix2 );
for ( int[] vector : matrix3 )
{
for ( int num : vector )
{
System.out.print( num );
}
System.out.println();
}
System.out.println();
//N-Array
System.out.println( Arrays.deepToString( (Object[])ArrayN.addArraysN( (Object)matrix1, (Object)matrix2, 2, 5 ) ) );
}
public static int[] addVectors( int[] a, int[] b )
{
int[] c = new int[a.length];
for ( int i = 0; i < a.length; i++ )
{
c[i] = a[i] + b[i];
}
return c;
}
public static int[][] addMatrices( int[][] a, int[][] b )
{
int[][] c = new int[a.length][a[0].length];
for ( int i = 0; i < a.length; i++ )
{
c[i] = ArrayN.addVectors( a[i], b[i] );
}
return c;
}
public static Object addArraysN( Object arrayN1, Object arrayN2, int dimension, int innerlength )
{
if ( dimension == 0 )
{
return (int)arrayN1 + (int)arrayN2;
}
else
{
int[] dimensions = new int[dimension];
for ( int i = 0; i < dimension; i++ )
{
dimensions[i] = innerlength;
}
Object arrayN3 = Array.newInstance( Array.class, dimensions );
for ( int i = 0; i < Array.getLength( arrayN1 ); i++ )
{
Array.set( arrayN3, i, ArrayN.addArraysN( Array.get( arrayN1, i ), Array.get( arrayN2, i ), dimension-1, innerlength ) );
}
return arrayN3;
}
}
}
Output:
44444
44444
44444
Exception in thread "main" java.lang.IllegalArgumentException: array element type mismatch
at java.lang.reflect.Array.set(Native Method)
at ArrayN.addArraysN(ArrayN.java:85)
at ArrayN.addArraysN(ArrayN.java:85)
at ArrayN.main(ArrayN.java:41)
appendix 2
I've found the error. It was the following line:
Object arrayN3 = Array.newInstance( Array.class, dimensions );
I had to replace Array.class
with int.class
. The corrected line should be:
Object arrayN3 = Array.newInstance( int.class, dimensions );
Now I realized another problem the code has:
Every array in the multidimensional array has to be of the same size because of the innerlength argument. If arrays are shorter the other values become zero:
44444
44444
44444
[[4, 4, 4, 4, 4], [4, 4, 4, 4, 4], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
So I first made matrix1
and matrix2
a bit longer:
//Matrix
int[][] matrix1 = {{0, 1, 2, 3, 4}, {4, 3, 2, 1, 0}, {0, 1, 2, 3, 4}, {4, 3, 2, 1, 0}, {0, 1, 2, 3, 4}};
int[][] matrix2 = {{4, 3, 2, 1, 0}, {0, 1, 2, 3, 4}, {4, 3, 2, 1, 0}, {0, 1, 2, 3, 4}, {4, 3, 2, 1, 0}};
But that's not a good solution.
Nikoloz wrote a method to find out the dimensions of arrays. Using it and another method arrayToString( Object )
I wrote my final code is now:
import java.util.Arrays;
import java.lang.reflect.Array;
import java.util.List;
import java.util.ArrayList;
public class ArrayN
{
public static void main( String[] args )
{
int[][] matrix1 = {{0, 1, 2, 3, 4}, {4, 3, 2, 1, 0}};
int[][] matrix2 = {{4, 3, 2, 1, 0}, {0, 1, 2, 3, 4}};
System.out.println( ArrayN.arrayToString( ArrayN.addArraysN( matrix1, matrix2 ) ) );
}
public static Object addArraysN( Object arrayN1, Object arrayN2 )
{
ArrayList<Integer> dimensions = new ArrayList<Integer>();
ArrayN.getDimensions( arrayN1, dimensions );
int[] dims = new int[dimensions.size()];
for ( int i = 0; i < dims.length; i++ )
{
dims[i] = dimensions.get( i );
}
if ( dims.length == 0 )
{
return (int)arrayN1 + (int)arrayN2;
}
else
{
Object arrayN3 = Array.newInstance( int.class, dims );
for ( int i = 0; i < Array.getLength( arrayN1 ); i++ )
{
Array.set( arrayN3, i, ArrayN.addArraysN( Array.get( arrayN1, i ), Array.get( arrayN2, i ) ) );
}
return arrayN3;
}
}
public static void getDimensions( Object array, List<Integer> dimensions )
{
if ( array != null && array.getClass().isArray() )
{
dimensions.add( Array.getLength( array ) );
if ( Array.getLength( array ) > 0)
{
ArrayN.getDimensions( Array.get( array, 0 ), dimensions );
}
}
}
public static String arrayToString( Object arr )
{
if ( arr instanceof byte[] )
return Arrays.toString( (byte[])arr );
else if ( arr instanceof short[] )
return Arrays.toString( (short[])arr );
else if ( arr instanceof int[] )
return Arrays.toString( (int[])arr );
else if ( arr instanceof long[] )
return Arrays.toString( (long[])arr );
else if ( arr instanceof float[] )
return Arrays.toString( (float[])arr );
else if ( arr instanceof double[] )
return Arrays.toString( (double[])arr );
else if ( arr instanceof char[] )
return Arrays.toString( (char[])arr );
else if ( arr instanceof boolean[] )
return Arrays.toString( (boolean[])arr );
else
return Arrays.deepToString( (Object[])arr );
}
}
Another possibility would be to take dimension 1 as the base case:
public static Object addArraysN( Object arrayN1, Object arrayN2 )
{
ArrayList<Integer> dimensions = new ArrayList<Integer>();
ArrayN.getDimensions( arrayN1, dimensions );
int[] dims = new int[dimensions.size()];
for ( int i = 0; i < dims.length; i++ )
{
dims[i] = dimensions.get( i );
}
if ( dims.length == 1 )
{
Object arrayN3 = Array.newInstance( int.class, dims );
for ( int i = 0; i < Array.getLength( arrayN1 ); i++ )
{
int sum = ((int[])arrayN1)[i] + ((int[])arrayN2)[i];
Array.set( arrayN3, i, sum );
}
return arrayN3;
}
else
{
Object arrayN3 = Array.newInstance( int.class, dims );
for ( int i = 0; i < Array.getLength( arrayN1 ); i++ )
{
Array.set( arrayN3, i, (int[])ArrayN.addArraysN( Array.get( arrayN1, i ), Array.get( arrayN2, i ) ) );
}
return arrayN3;
}
}
Related questions
How to sum arrays in Java
Is it possible to dynamically build a multi-dimensional array in Java?
Iterating over arrays by reflection
Java Reflection - Get size of array object
Creating an n-dimension Array in Java during runtime
Initialising a multidimensional array in Java
finding sum of two dimensional array java
Adding matrices Java
Java Matrices Arrays