I have a boolean array that stores CheckBox values ( true or false ). In case all elements of the array are true , I need to perform some operation. So, how to check the array to true in all elements? It is necessary to check for the presence of all true . I tried this:
boolean[] checked = { true, true, false, true }; for (int z = 0; z < checked.length; z++) { if (checked[z] == false) System.out.println("в массиве есть false!"); } It turns out that at every encounter false in the array, the program will show a message. But I need the opposite - showing the message in the absence of false . I add else :
else System.out.println("в массиве только true!"); This way I will get the message to be repeated as many times as true in the array. I need to perform the operation only once, in case there is only true in the array!
UPD A little thought, I wrote this:
int x = 1; for (int z = 0; z < checked.length; z++) { x = x * ((checked[z] == true) ? 1 : 2); } if (x == 1) { //мои действия } I use the ternary operator in each iteration to multiply one by itself (if true ) or by two (if false ). As a result, if the resulting number is one, it means that only true in the array.
false, otherwisetrue. Are you familiar with the language? - D-side