They are logical, so I really don’t know why you think about converting them to integers and summing them up: it would be very easy, but definitely useless.
So. I assume that you do not have them in the matrix (I really assume that you do not know what the matrix is, but please do not be offended by this), you have them in separate arrays, and you know exactly how many arrays you have (say 3). I also assume that they all have the same length.
Idea: for each available position, we check three elements occupying this position in three arrays, and see if they are all true. What can we do with the result of this comparison? We can print it on the screen, put it in another array, or whatever we do.
Let's start with three arrays:
boolean[] foo = {true, true, false}; boolean[] bar = {true, false, false}; boolean[] baz = {true, false, false};
Ok Now we can create a fourth array to store our results.
boolean[] qux = new boolean[3];
or even better:
boolean[] qux = new boolean[foo.length];
Let's start building our C-style loop:
for (i = 0; i < foo.length; i++) { ... }
If you work with arrays, you probably already know how the for loop works: the first statement will be executed as if it were written before the entire block of code in curly braces; the second statement is checked as the first statement every time a block is executed, and in case of failure, the cycle stops; the third statement will be executed as the last command each time the block is executed.
And you probably already know that if the array is of length n , the indices for its elements will be
0, [...], n-1 .
If you do not know these two things ... Well, please.
So what did I say that we want to do with every available position? We want to compare the three elements in this position. Thus, the for loop becomes:
for (int i = 0; i < foo.length; i++) { if (foo[i] && bar[i] && baz[i]) { ... } }
We added this check: if the element at position i in the array foo is true , AND ALSO , the element at position i in the array bar is true , AND ALSO the element at position i in the array baz is true .. We are doing something.
We can, for example, put the result in qux :
for (int i = 0; i < foo.length; i++) { if (foo[i] && bar[i] && baz[i]) { qux[i] = true; } else { qux[i] = false; } }
Please note that the comparison itself is evaluated as logical, and when its value is true , we keep true ; when false store false . Therefore, we can also write:
for (int i = 0; i < foo.length; i++) { qux[i] = (foo[i] && bar[i] && baz[i]); }
Grrrrreat, we have achieved what we want to achieve! Do you also want to display this result? Let me introduce you to the extended loop:
for (boolean val : qux) { ... }
Basically, being a qux array of booleans, it is a “container” of boolean values. This statement says: for every element making up qux ... do something.
So, let's use it to print these elements:
for (boolean val : qux) { System.out.println(val); }
The println() method, in the out object, is a static member of the System class (do not worry if you do not understand all these terms, you will find out their meaning if you continue to learn Java) something will be displayed on the screen. Technically, this “something” should be a String, but since Java knows how to make strings from booleans, we don’t have to worry too much when passing val.
Want to do everything in the first, and also print beautifully? Easy as a pie:
for (int i = 0; i < foo.length; i++) { qux[i] = (foo[i] && bar[i] && baz[i]); System.out.println("At position " + i + " we have " + qux[i]); }
So how do we now check all false positions? We can use bang ( ! ), The meaning of which is to take this boolean and consider its negation ( true if it is false and false if it is true ).
Using this, we can express this check: if the element at position i in the array foo is false , AND the ALSO element at position i in the array bar is false , AND ALSO the element at position i in the baz array is false ... We then do it.
So, we need the fifth array:
boolean[] norf = new boolean[foo.length];
And we would change our cycle to
for (int i = 0; i < foo.length; i++) { qux[i] = (foo[i] && bar[i] && baz[i]); if (qux[i]) { System.out.println("At position " + i + " they are all true."); } norf[i] = (!foo[i] && !bar[i] && !baz[i]); if (norf[i]) { System.out.println("At position " + i + " they are all false."); } }
Hope this was helpful.