Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: where I'm wrong?

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default 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
    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??
    Last edited by arvindbis; March 7th, 2012 at 03:17 AM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: where I'm wrong?

    Paste the exception/errors message here.
    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?

  3. The Following User Says Thank You to Mr.777 For This Useful Post:

    arvindbis (March 7th, 2012)

  4. #3
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Question 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.

  5. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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?

  6. #5
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default 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...

Similar Threads

  1. Idk what is wrong
    By Devourz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2011, 05:05 AM
  2. WHAT IS WRONG HERE PLEASE?
    By mjava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 27th, 2010, 08:29 PM
  3. Not sure what is wrong with this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 29th, 2010, 02:23 PM
  4. don't know what's wrong
    By james in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 15th, 2010, 07:37 PM