There is a table of zeros and ones.
{1, 1, 0, 0, 0, 0, 0, 0}, {1, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 1, 1, 0, 0, 0, 0}, {1, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 1, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 1, 1} From it you need to remove:
- all columns, where only one unit,
- all lines that intersect with them, crossing this unit,
- all columns that had a unit at the intersection with these rows.
That is, using the example of the table above, it was necessary to delete the following:
{1, 1, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 1, 1}
What should remain:
{0}, {1}, {1} Implemented with ArrayList. Initially, I wanted to delete all columns through ArrayName.get(i).remove(j); but for reasons I don't understand, everything worked extremely incorrectly.
I decided to implement it with a small crutch: I delete rows that need to be deleted, transpose, delete columns that have become rows. In general terms, this is a working scheme, but when I got to the specifics (a million conditions for deleting and searching for these rows and columns), problems started again.
Naturally, I brought the problem point to a separate project and started looking for an error, bringing the code to the simplest version. And I noticed this:
Suppose I have a two-dimensional List
List<List<Integer>> FirstTableInt = new ArrayList<>(); And there is a certain “mask” - an array of the form {1, 0, 0, 0, 1, 1, 1} , in which the units correspond to the rows that need to be removed from the two-dimensional array. Perhaps a stupid option, but so I came up with :)
All checks pass correctly, the correct "mask" is recorded there. I try to delete lines as follows:
for (int i = 0; i < stolbci_removeL.size(); i++) if (stolbci_removeL.get(i).equals(1)) { FirstTableInt.remove(i); stolbci_removeL.remove(i); } Where, respectively, FirstTableInt is a two-dimensional List, from where you need to remove rows, and stolbci_removeL is an array with a "mask". What do I observe at work? There is an initial "mask": {1, 1, 1, 1, 0, 1, 1, 1}
When passing the stolbci_removeL.get(i).equals(1) condition, the following row indices should be selected: {0, 1, 2, 3, 5, 6, 7} , but for some reason, {0, 1, 3, 4} , and what is most interesting , if I change the condition to stolbci_removeL.get(i).equals(0) , everything will be selected correctly - there will be only {4} .
Why and under the condition .equals(1) and under the condition .equals(0) is this cell selected? What a magical data comparison?
For reference: stolbci_removeL is of type Integer , I compare its cells through equals , although through == result is the same.
What is the problem? I tried experimenting with the int and Integer types, and I know that the indices in the List and ArrayList are always of the int type, and I took that into account. But I do not understand why an incorrect comparison occurs, and under interdependent conditions the result overlaps! The results of the conditions !stolbci_removeL.get(i).equals(0) and stolbci_removeL.get(i).equals(1) same.