Results 1 to 2 of 2
  1. #1
    nvwin is offline Member
    Join Date
    May 2016
    Posts
    6
    Rep Power
    0

    Default 3D multiplication

    I have the java code bellow for 3D multiplication , i have an error .


    Java Code:
    import java.util.*;
    
    class test27
    {
    
    public static void main ( String[] args ) {
    
    
    	int [][][] arr1 = {{{6,3,9,0},{8,6,5,3},{9,0,7,5}}};
    	int [][][] arr2=  {{{6,3,9,0},{8,6,5,3},{9,0,7,5}}};
    
    	test27 test = new test27();
    
    	    System.out.println(Arrays.deepToString(test.multiply3D(arr1,arr2)));
    
    }
    
    
    public static int[][][] multiply3D(int[][][] array3D, int[][][] arr3D) {
    
    		int len =  array3D.length;
    		int array3D_row = array3D[0].length;     //      m
    		int array3D_col = array3D[0][0].length;   //     n
    
    		int arr3D_row = arr3D[0].length;      //     p
    		int arr3D_col = arr3D[0][0].length;    //     q
    
    		int i, j, k;
    
    
    		int [][][] ThreeDmultiply = new int[len][array3D_row][array3D_col];
    		int  sum = 0;
    
    		for (int s=0; s < len ;s++){
    
    
    			for ( int v = 0 ; v < array3D_row ; v++ )
    			{
    				for (int  d = 0 ; d <arr3D_row ; d++ )
    				{
    					for (int  u = 0 ; u < arr3D_col ; u++ )
    					{
    						sum = sum + array3D[s][v][u]*arr3D[s][u][d];
    
    					}
    
    					ThreeDmultiply[s][v][d] = sum;
    					sum = 0;
    				}
    			  }
    		   }
    return ThreeDmultiply;
    
    
    }
    }
    The error :

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

    PLEASE ANY SUGGESTION
    Last edited by nvwin; 05-17-2016 at 11:09 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: 3D multiplication

    Please copy the full text of the error message and paste it here. It has important info about the error.

    Note: You don't need an instance of a class when calling a static method in that class. Line 12 can be removed.

    Why are there 4 nested loops to handle a 3D array?

    The variables in lines 21-26 make the code harder to read and understand. The array length references should be used directly in the for statements:
    Java Code:
     for (int s=0; s < array3D.length ;s++){
    Last edited by Norm; 05-17-2016 at 01:01 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Galois Multiplication
    By christopherx in forum Advanced Java
    Replies: 1
    Last Post: 01-04-2012, 02:13 PM
  2. int array multiplication
    By sternhagenr in forum New To Java
    Replies: 10
    Last Post: 08-20-2011, 01:41 AM
  3. need help with multiplication
    By dakid2 in forum New To Java
    Replies: 10
    Last Post: 03-08-2011, 03:41 AM
  4. Help with Multiplication
    By phil028 in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 07:39 PM
  5. Help with multiplication table
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 04:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •