# where I'm wrong?

• March 6th, 2012, 08:50 PM
arvindbis
where I'm wrong?
i'm trying to develop a code to add two matrixes and then store result in another matrix but i use to get arrayoutofbounds expection every time i try to run it. please let me know where I'm going wrong?
have a look at my code
Code :

```public class MatrixAdd { static int[][] test1a = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; static int[][] test1b = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; static int[][] test2a = {{1,2,3,4,5,6,7,8},{9,10,11,12,13,14,15,16}}; static int[][] test2b = {{1,2,3,4,3,2,1,0},{5,6,7,8,9,10,11,12}};   static int[][] testcaseA = test2a; static int[][] testcaseB = test2b;   public static void main(String args[]){ MatrixAdd inst = new MatrixAdd(); int[][] v = inst.add(testcaseA,testcaseB); System.out.println(v); }   // Write your code in the function below public int[][] add(int[][] m1,int[][] m2){   int result[][];   int lb=m2.length; result = new int[lb][lb]; for(int i = 0; i <lb; i++) for(int j = 0; j <m2[i].length; j++) result[i][j]=m1[i][j]+m2[i][j]; Eror is here. return result; } }```

i'm not getting it where i'm wrong??
• March 7th, 2012, 04:58 AM
Mr.777
Re: where I'm wrong?
Paste the exception/errors message here.
Code :

`result = new int[lb][lb];`
lb = 2 and result is now result[2][2]. Why do you expect it, not to throw array out of bound exception?
• March 7th, 2012, 07:35 AM
arvindbis
Re: where I'm wrong?
thank you Mr. 777
I understood what you told. Can you please tell me how can i find size of array of array.
i.e. if array is of size 2x4 then array.length only gives 2 as result, but as we know the very first element is pointing to another array of size 4. then how to find this at runtime as size of array is changed while providing input to this program.
• March 7th, 2012, 07:48 AM
Mr.777
Re: where I'm wrong?
And what array[index].length will return you? Don't you think, it'll return the array size of what you called array of array?
• March 7th, 2012, 08:12 AM
arvindbis
Re: where I'm wrong?
yes i thought about that but after posting that here, and i don't know where is link to delete function...=D>