views:

52

answers:

2

how can i say in java

if i get to the end of a row in a 2d array and i have only encountered 0's in that row to set row to the next one (row ++) and set column to 0 (column = 0)?

i am having problems where my algorithm i going in to an infinite loop :D

thanks

+1  A: 

uh, sounds like you want a basic nested loop to iterate across your array...

for(int i=0; i<rows.length; i++)
   for(int j=0; j<columns.length; j++)

This will reset your columns regardless. If you want to do something tricksy and hackish

int sum =0;
for(int i=0; i<rows.length; i++)
{
  sum+= rows[i];
  if(i == rows.length-1 && sum == 0)
  {
     i=i+1
  } 
   for(int j=0; j<columns.length; j++)
   {
     //whatever else you want here
   }
}

Note that this seems like a most unusual condition algorithmically...I hope you're not trying to hack around a dirty bug instead of figuring out what the real problem is!

Visionary Software Solutions
i have a symmetrical matrix, when i loop accross the top of it i find a >0 , i take one away from matrix[x][y] and matrix[y][x] then i set the next row to the column i was in, this happens until there are no values left in my array, problem is as i do this i get to a point where a row has no 0's in it therfore i need to say, if i am at the end of a row and there is no 0's, just start afresh from the next
timmy
So what exactly are you trying to do? If you're trying to dynamically resize an nxn two-dimensional array within a loop, you're probably going to get yourself in trouble. It sounds pretty esoteric, but if you're just trying to simultaneously reduce the value in [2][3] and [3][2] by 1 while there are still non-zero values in your array, you can simplify the logic for that quite a bit.
Visionary Software Solutions
yes this is all i am tyring to do, it works fine for the snaller graphs, i just tried to make a beast and it gets stuck in a loop :S
timmy
In that case, why not iterate through the array normally, and calculate the difference of [x][y] and [y][x], and store that in whichever one was bigger in the first place? that's going to be the end result of your shakeup anyway. If your ultimate goal is to just have an nxn array of 0, there's an easier way to do that too... :)
Visionary Software Solutions
A: 

This sample loops through the array until the first non-zero value is found and outputs it along with the coordinates.

int[][] x = new int[column][row];
boolean allzero = true;

for(int i=0;i<row;i++) {
  for(int j=0;j<column;j++) {
    allzero = (x[j][i]==0) && allzero;
    if(!allzero) {
      //what do we want to do when we encounter a non zero value?
      System.out.println("Found "+x[j][i]+" at row: "+i+" column: "+j);
      break;
    }
  }
  if(!allzero) {
    //what do we want to do when we encountered a non zero value?
    break;
  }
}

Actually I don't understand what you mean with

set column to 0 (column = 0)

If you just want to continue looping through the array two nested for-loops like the above will do it. After one row is finished (inner-loop) the outer-loop increments by one and the inner-loop restarts at zero. Thus all rows are run through from start to end.

jitter