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.

  • Lord ... Use a two-dimensional array! - Flippy
  • Give the working code with an example. And remove a cloud of unnecessary information. - And
  • 2
    "all the rows that intersect with them" - so you have any column intersects with all the rows. It turns out that if you delete one column (since there is only one unit in it - for example, the second one), then you need to delete all the rows and there will be nothing left in the matrix immediately. Something is wrong on this point. - Regent

1 answer 1

 for (int i = 0; i < stolbci_removeL.size(); i++) if (stolbci_removeL.get(i).equals(1)) { FirstTableInt.remove(i); stolbci_removeL.remove(i); } 

Do not do this ever. You go through the list with iterator i , and in the loop delete the elements right away. What will happen? You will change the size of the list you are going through on each deletion; you will skip the items you need to check.

Consider a primitive example, you have a list of numbers, you need to delete all odd numbers:

 1 2 5 7 9 14 13 16 for (int i = 0; i < list.size(); i++) if (list.get(i) % 2 == 1) { list.remove(i); } 

So, in steps:

исходный массив: 1 2 5 7 9 14 13 16, поехали i = 0: По индексу 0 у нас число 1, нечетное, удаляем, остается: 2 5 7 9 14 13 16 i = 1 по индексу 1 у нас 5, удаляем (на двойку мы даже не глянули, она теперь с индексом 0): 2 7 9 14 13 16 i = 2 по индексу 2 у нас 9, удаляем (прозевали 7): 2 7 14 13 16 i = 3 по индексу 3 у нас 13, удаляем: 2 7 14 16 list.size() == 4, i == 4, выходим из цикла.

I hope you understand the problem? Usually, in such cases, an additional array is made into which all indexes are written, for which the delete operation is to be performed, and then everything else is deleted in another cycle.