Results 1 to 8 of 8
  1. #1
    Rederich is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default 2D Array transposition

    Hey all, im getting an "array index out of bounds error" but dont see why i would be getting it...

    here is the code:
    Java Code:
     int[][] transpose(int [][] a) {
    
            int n = a.length;
            int m = a[n-1].length;
            int [][]b = new int [m][n];
            for(int i=0;i<n;i++){    
                for(int j=0;i<m;j++) {
                 
                   b[j][i]=a[i][j]; <-- this is where im getting the error at, although i dont see how im going out of bounds..:frusty:
                       
                }
            }
            return b;
        }
    I would greatly appreciate it if someone could help me out, thank you.
    Last edited by JosAH; 12-02-2011 at 08:00 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: 2D Array transposition

    Is array 'a' a 'ragged' array? e.g. int[][] a= { { 1, 2 }, { 3, 4, 5 }}; in this array not all rows have the same size and it ruins your transpose( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Rederich is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: 2D Array transposition

    We are to assume that all the rows are of equal length. Sorry, i should have mentioned that. Just to keep things simple ive been assigning the parameter to be a square matrix, but im still getting the error.

  4. #4
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: 2D Array transposition

    i think the condition of the second for loop is incorrect...take a look

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: 2D Array transposition

    Quote Originally Posted by Rederich View Post
    We are to assume that all the rows are of equal length. Sorry, i should have mentioned that. Just to keep things simple ive been assigning the parameter to be a square matrix, but im still getting the error.
    I only see your mistake now: check the conditional expression in your inner loop; it should be j instead of i ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Rederich is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: 2D Array transposition

    Thanks for pointing that out. I should really pay attention to detail...XD.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: 2D Array transposition

    Quote Originally Posted by Rederich View Post
    Thanks for pointing that out. I should really pay attention to detail...XD.
    You're welcome of course; your nasty little mistake was hard to spot; a tip: add the tag [code] above your Java code and put a [/code] tag just below it; I did the same in your original post to make the code more readable.

    kind regards.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

Similar Threads

  1. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  2. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

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