Measurement of overlap between arrays

Given several arrays in Java (I will iterate over the keys currently stored in the HashMap), I want to be able to identify (based on the currently stored boolean[] keys) whose indices are true in all of them, and which are false .

Example:

 {true, true,false} {true,false,false} {true,false, false} 

will give index 0 as having all true values, and index 2 will have all false values.

One of my ideas is to convert a logical array to an ints array, summarize the values, and if the total index of the array is = numArrays, then it is True in all of them. Similarly, if the total index of the array is 0, it is false in all of them.

Not sure how to do this efficiently (in java), letalone if this is a good method to achieve the desired result.

Is there a way to convert an array of booleans to ints? If not, is this the easiest alternative to instantiate a new int array and a circular boolean array to populate the new array?

+5
source share
3 answers

Assuming all arrays will have the same number of elements, you can try the following:

 public static void main(String[] args) { boolean[] array1 = new boolean[] {true,true,false}; boolean[] array2 = new boolean[] {true,false,false}; boolean[] array3 = new boolean[] {true,false,false}; boolean[] array4 = new boolean[] {true,false,false}; compareAndPrintCommonIndicesForArrays(array1, array2, array3); } public static boolean compareAndPrintCommonIndicesForArrays(boolean[] ...arrays) { boolean result =true; boolean itemToCompare; int elementsToCompare = arrays[0].length; for(int itemIndex=0; itemIndex<elementsToCompare; itemIndex++) { result = true; itemToCompare = arrays[0][itemIndex]; for(int arrayIndex = 0; arrayIndex<arrays.length;arrayIndex++) { if(itemToCompare != arrays[arrayIndex][itemIndex]) { result = false; } } if(result) System.out.println(itemIndex); } return result; } 

He prints:

0 2

The code will intersect all indexes, and for each index, it will compare elements from the entire array in that index. If they are all the same, the index is printed.

+1
source

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.

+2
source

Basically, you want to wrap arrays in another array, create a 2D matrix, for example ...

 boolean matrix[][] = { {true, true, false}, {true, false, false}, {true, false, false} }; 

Then for each column in each row you want to check if the values ​​match or not

 boolean results[] = new boolean[3]; for (int col = 0; col < 3; col++) { Boolean value = null; boolean matches = true; for (int row = 0; row < matrix.length; row++) { if (value == null) { value = matrix[row][col]; } else if (matrix[row][col] != value) { matches = false; break; } } results[col] = matches; } System.out.println(Arrays.toString(results)); for (int col = 0; col < results.length; col++) { if (results[col]) { System.out.println(col); } } 

What prints ...

 [true, false, true] 0 2 

The reason for the boolean results[] array is that each column will either match ( true ) or not ( false ), it's just checking each element to determine which indexes result in true result

0
source

Source: https://habr.com/ru/post/1232594/


All Articles