1 2 Previous Next 21 Replies Latest reply: Feb 9, 2009 5:34 PM by 699554

# Traversing a 2d array...

So I have been staring at code for awhile and can't seem to understand why this is not working. The code is designed to look at a 2d array and add the rows and the columns and say which row/column has the greatest sum.

This is for adding the rows
for(int i = 0; i < mat.length; i++)
{
sumOfRow = 0;
for(int j = 0; j < mat.length; j++)
{
sumOfRow = sumOfRow + mat[i][j];
}
}
and this is for the colums
for(int i = 0; i < mat.length; i++)
{
sumOfCol = 0;
for(int j = 0; j < mat[i].length; j++)
{
sumOfCol = sumOfCol + mat[j][i];
}
}
my eyes can no longer stare into the eyes of this code! So when I have a 2d array of 4x4 it works perfectly fine. When they are not squares it seems to go out of bounds...but when looking at the code it looks as though this should work and I can't find my error :(.

Any help would be appreciated,

• ###### 1. Re: Traversing a 2d array...
Compare the inner for-loop of both methods. In both you are using "mat". Is that the correct approach?
• ###### 2. Re: Traversing a 2d array...
I am confused lol. I am pretty sure yes...I can never tell if people are asking me a rhetorical question or not.

Are you asking because you see the problem and you are giving me a hint or are you asking because you are just asking....?

Re-reading that just confused me more, I need sleep, haha.
• ###### 3. Re: Traversing a 2d array...
mindloss wrote:
I am confused lol. I am pretty sure yes...I can never tell if people are asking me a rhetorical question or not.
Try to answer it, that helps ;-)
Are you asking because you see the problem and you are giving me a hint or are you asking because you are just asking....?
It was not rethorical.
Re-reading that just confused me more, I need sleep, haha.
If you think you need some sleep, then you're probably right.

I'll try a different approach: How do you find out how many columns such an array has? Can you find that without making any other assumptions about the array?
• ###### 4. Re: Traversing a 2d array...
Well for the problem I am solving the array is rectangular so to speak...so all the columns have the same numbers of elements. So I thought the easiest way to do that was to do array[index].length. (This is why I am using i).
• ###### 5. Re: Traversing a 2d array...
mindloss wrote:
Well for the problem I am solving the array is rectangular so to speak...so all the columns have the same numbers of elements.
Ok, so that's an important point. Otherwise you'd be hard pressed to make a valid implementation.
So I thought the easiest way to do that was to do array[index].length. (This is why I am using i).
Think about it this way. To get the sum of each row you basically do:
for each row in array:
for each collumn in row:
<do something>
That looks fine.

For the sum of the columns you do this:
for each row in array:
for each collumn in row:
<do something>
Anything look fishy when you compare those two?
• ###### 6. Re: Traversing a 2d array...
JoachimSauer wrote:
Compare the inner for-loop of both methods. In both you are using "mat". Is that the correct approach?
If your still confused about what JoachimSauer was meaning here i advice you to draw up a grid with 4 rows but unequal columns. Then apply your second algorithm to this. If you use ".length", physically draw that on the grid, then come back and tell us what the problem is. Like JoachimSauer said its got something to do with your inner for loop.

Mel
• ###### 7. Re: Traversing a 2d array...
I mean I see what you are saying and I see that, but then I look at the code again and it still makes sense lol.

I tried this way because it makes sense when comparing to how rows are done, but it doesn't work for any case...so I am just stuck, possibly even working backwards.
for(int i = 0; i < mat.length; i++)
{
sumOfCol = 0;
for(int j = 0; j < mat.length; j++)
{
sumOfCol = sumOfCol + mat[i][j];
}
}

• ###### 8. Re: Traversing a 2d array...
Wow now looking at my rows it doesn't even make sense anymore to me. I wish java used a plane like in math...I think the backwards-ness of this is really messing me up.
• ###### 9. Re: Traversing a 2d array...
[0,0] [0,1] [0,2] [0,3]
[1,0] [1,1] [1,2]
[2,0] [2,1] [2,2] [2,3]
[3,0]
Think of a 2D array as one array down the first column, now each element in this first column references an array across its row. So you want to iterate down the rows first, find the length of the row so you dont get arrayoutofbounds exception then iterate across its columns.

Down then Across

Mel
• ###### 10. Re: Traversing a 2d array...
mindloss wrote:
Wow now looking at my rows it doesn't even make sense anymore to me.
I wish java used a plane like in math...
That's why "real" programs usually don't handle raw arrays.

They use (or write) classes that represent exactly the abstraction that they would like to use.

You could easily write a class like this:
public class Matrix {
public Matrix(int rows, int columns) {
// create the internal array
}

public int getField(int row, int col) { ... }
public void setField(int row, int col, int value) { ... }
public int getRows();
public int getColumns();
}
Using such a class would be a lot more intuitive than trying to do serious work without the correct abstraction (i.e. using Java arrays directly).

It could also ensure that the matrix is actually rectangular (and not a jagged array).
• ###### 11. Re: Traversing a 2d array...
>

Using such a class would be a lot more intuitive than trying to do serious work without the correct abstraction (i.e. using Java arrays directly).

It could also ensure that the matrix is actually rectangular (and not a jagged array).
I wish we could be creative like that. I am extremely limited in what I can use/create. The input is given in arrays so I have to create the code to find the maxSum :P...or else I would find a much better way to do this.

So I have been staring at the code and writing it out on paper. I just don't understand how given an array that is a square, the code works perfectly fine...it iterates through the array just like I want it to and I get the answers correct.

How is it that the only thing messing it up is the bounds. I have tried everything with the bounds and nothing seems to make it do what I want it to. The only way (that seems like it should work) messes up even when given a perfect square.

/stuck
• ###### 12. Re: Traversing a 2d array...
Meh arrays are a great learning source imo. Btw your for loops would benefit from this condition.
if (condition){
increment;
}
else{
continue;
}
E.g. if you want to sum column 3, but lets say row 2's width is only 2 then you dont want to access that value since its out of bounds, you just want to continue with your inner loop.
• ###### 13. Re: Traversing a 2d array...
Melanie_Green wrote:
E.g. if you want to sum column 3, but lets say row 2's width is only 2 [...]
I think the OP said that for his example he can assume a rectangular array (i.e. not jagged).

Ah, there it is:
mindloss wrote:
Well for the problem I am solving the array is rectangular so to speak...
• ###### 14. Re: Traversing a 2d array...
Melanie_Green wrote:
Meh arrays are a great learning source imo. Btw your for loops would benefit from this condition.

E.g. if you want to sum column 3, but lets say row 2's width is only 2 then you dont want to access that value since its out of bounds, you just want to continue with your inner loop.
They are all rectangular so I can't have that problem if I am understanding you right...
1 2 Previous Next