i have this code. i am trying to set a grid and start at c. it then moves to the next value - 1, so in this case, would move to a 4.
However, in my IF statement, i want to put if(position surrounding c == c-1){then change that position to c; then c-1;}
i dont know how to specify the position without hardcoding it i.e. [2][1]
is there a way to do so?

EDIT : another way of putting it is ... is there a way to search my array for the c-1 in my IF

public class Array{

  public static void main(String[] args) {
    int c = 5;
  int[][] startPos =
  {{3,4,c,6,7},
   {2,3,4,6,7},
   {1,2,3,6,7},
   {0,1,2,6,7}};
  
  for (int row = 0; row < startPos.length ; row++ ) {
    for (int col = 0; col < startPos[row].length; col++ ) {
     System.out.print(startPos[row][col]);
    }
    System.out.println();
   }
  if(startPos[0][1] == c-1){
    startPos[0][1] = c;
  }
  for (int row = 0; row < startPos.length ; row++ ) {
    for (int col = 0; col < startPos[row].length; col++ ) {
     System.out.print(startPos[row][col]);
    }
    System.out.println();
   }
}
}

Recommended Answers

All 2 Replies

If c starts in the same place always, int[0][2] in the example given, then keep two ints x = 0, y = 2. if c == startPos[x][y-1] then y=y-1. Now startPos[x][y] is the previous value, 4 in the example given.

Is that what you're asking?

yeah thats kind of what i wanted, i used your code to inspire me and came up with

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 5; j++)
  if(startPos[i][j] == c-1){
    startPos[i][j] = c;
    c = c-1;
    System.out.println();

that's lists through every co-ordinate in the grid and finds every value lower then c

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.