Saturday 15 May 2010

Testing for equality in a two-dimensional array in Java -


How do I find all the matching elements in a two-dimensional array in Java?

In fact you simply repeat through rows and columns, check whether the contents of the addressed cell are both The matrix is ​​similar (!) And the result is another matrix, which is the result of your operation.

Do not forget to apply the mandatory check for the matrix, else the algorithm will definitely crash if you provide 'illegal logic'.

Variation: If you need Java primitives (int or float), change the type of array and do not use equal to compare but == operator. Personal Boolean [] Find matches (object [] [] array 1, object [] [] array 2) {if not (not comparable (array 1, array 2) {returns tap;} boolean [] [] results = new boolean [ Array1.length, array1 [0], length]; (int row = 0; row & lt; array1.length; line ++) {for (int column = 0; column & lt; array1.length; column ++) {If (array1 [row] [column] .equals (array2 [row] [column]) {result [line] [column] = true;}}} return result;} private boolean not identical (object [] [] array1 , Object [] [] array 2) {// dummy implementation - add here your check to guarantee here Make sure the arrays are not empty, empty and return false in each line in the same size;}


No comments:

Post a Comment