Results 1 to 8 of 8
Thread: 2D Array transposition
- 12-02-2011, 08:39 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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:
I would greatly appreciate it if someone could help me out, thank you.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; }Last edited by JosAH; 12-02-2011 at 08:00 PM.
- 12-02-2011, 10:49 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-02-2011, 06:04 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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.
- 12-02-2011, 06:11 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 19
- Rep Power
- 0
Re: 2D Array transposition
i think the condition of the second for loop is incorrect...take a look
- 12-02-2011, 06:13 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
- 12-02-2011, 06:43 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: 2D Array transposition
Thanks for pointing that out. I should really pay attention to detail...XD.
- 12-02-2011, 08:03 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Re: 2D Array transposition
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-02-2011, 08:13 PM #8
Member
- Join Date
- Dec 2011
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
How to convert array of Objects into array of Strings
By elenora in forum Advanced JavaReplies: 1Last Post: 06-10-2011, 03:48 PM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks