Remove repeated elements from a multidimensional vector

Asked

Viewed 168 times

0

I’m making a code to be able to remove duplicate lines from a vector with customer registrations.

This vector is "tabela[1000][3]"

So I have for example:

1ºlinha - "joao" " rua: asasdfasdfs" "cep: 21412354125"

2ºlinha - "matheus" " rua: asdfasdfasdf" "cep: 21412354125"

3ºlinha repetida - "jonas" " rua: asdfasdf" "cep: 21412354125"

4ºlinha - "guilherme" " rua: asdfasdfasdf" "cep: 21412354125"

5ºlinha - "gabriel" " rua: asasdfasdfasdfasdfs" "cep: 21412354125"

6ºlinha repetida - "jonas" " rua: asdfasdf" "cep: 21412354125"]

there are names of the first column that are repeated, and I would like to remove that row altogether, so I thought I would make a new vector only with non-repeated lines comparing row by row of the vector that would be filtered with the old:

int filt_next = 0;

    for(int atual = 0; atual < tabela.length; atual++){
        for(int passando = 0; passando < filtrado.length; passando++){

            String comp1 = tabela[atual][0];
            String comp2 = filtrado[passando][0];

            if(comp1 == comp2){

                break;

            }

            if(comp1 != comp2){

                filtrado[filt_next][0] = tabela[atual][0];
                filtrado[filt_next][1] = tabela[atual][1];
                filtrado[filt_next][2] = tabela[atual][2];


                filt_next++;

                break;
            }


        }

    }

The idea is that if the position of the first vector is equal to that of the second vector it would just break out of the loop, ignoring the repetition. And if it passes through all positions and is different it would add such a position in the new vector.

However the part that should take the equal positions is ignored, and the equal Strings are considered different, being added to the filter vector.

 if(comp1 == comp2){
    break;

}

I can’t understand what I’ve done wrong.

  • 1

    Comparison of Strings in java is with equals and not with ==. It’s still unclear what exact information the file has. And better solution would be as it reads lines from the file does not add to a client list if the client already exists, and then just have to rewrite the clients it has read

  • the table has 3 columns, and the first column has the name of the clients, I just need to compare this first column, without losing the order of the data of the other 2 columns... and not put the lines in a vector to compare it later?

1 answer

0

Whenever comparing strings you should use the equals method instead of == or !=. Replace your code to have:

if (comp1.equals(comp2)) {
    break;
}

if(!comp1.equals(comp2)){
    ...
    break;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.