Check if there is a specific value within a java bi-dimensional array

Asked

Viewed 65 times

0

I need to check if there is a specific value in case the value 1 within a bi-dimensional array for the code return if a value is true or false. The code I used was the following :

for (int i = 0; i < 10; i++) {

                        for (int j = 0; j < 10; j++) {

                            if (tabelaUser[i][j] == 1) {
                                ganhador2 = false;

                            } else {
                                ganhador2 = true;

                            }

                        }

                    }

but it didn’t work, I imagine my logic is wrong but I don’t know how to solve

  • specify better what you’re looking for, primitive, object... what type?

2 answers

0

What you’re doing wrong is using this else. And I’ll show you why.

Suppose your entire array has 3 numbers 1 0 1, when the for go through the first position of the array, it will verify that that is not the winner, because the number there is 1, in the second position it already determines that that is the winner because the value in that position is 0, however in the third position it determines that that is not the winner because the value in the last position is 1.

Note the representation of this flow in the winning variable 2: false -> true (is the winner) -> false

The solution to this is to remove the else and initialize your variable as true, if the for find the value 1 she will have her value changed.

Your code should look like this:

 boolean ganhador2 = true;

 for (int i = 0; i < 10; i++) {

      for (int j = 0; j < 10; j++) {
          if (tabelaUser[i][j] == 1) {
              ganhador2 = false;
          }

      }
}

0

This logic is wrong (I suggest you do the table test, it will be clear what is wrong).

Basically, you go through (I believe) all the elements, and if it is equal to 1, arrow the variable ganhador2 for false, and if different, arrow to true.

So actually what matters is the value of the last element found. If the latter is 1, the variable will be false, else she will be true, independent of all other values you have verified before.

If you are looking for an element, you start by indicating that you have not found it, and only change this statement if you find it. In general, it would be something like:

boolean encontrou = false; // antes do loop eu ainda não encontrei o elemento
for (int i = 0; i < elementos.length; i++) {
    if (elementos[i] == valorQueEstouProcurando) {
        encontrou = true;
        break; // encontrei, posso parar de procurar
    }
}

That is, if I found the element, I indicate that I found it (I change the value of the flag) and I may even interrupt the loop with break (because if I have already found, it makes no sense to keep looking).

But in your case there are two loops nested, and the break by default interrupts the most internal, so if you want to interrupt the for external just use a label:

boolean ganhador2 = true;
busca: for (int i = 0; i < tabelaUser.length; i++) {
    for (int j = 0; j < tabelaUser[i].length; j++) {
        if (tabelaUser[i][j] == 1) {
            ganhador2 = false;
            break busca; // encontrei, interrompe a busca
        }
    }
}

Notice I used length instead of a fixed value such as 10. So the code works for arrays of any size.

And if you just want to scroll through the values, you don’t have to iterate through the indexes, you can do so (assuming that tabelaUser be a int[][]):

boolean ganhador2 = true;
busca: for (int[] linha: tabelaUser) {
    for (int n: linha) {
        if (n == 1) {
            ganhador2 = false;
            break busca; // encontrei, interrompe a busca
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.