Remove repeated elements from a multidimensional vector

0

I'm making a code to be able to remove duplicate rows from a vector with customer records.

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 line altogether, so I thought of making a new vector with only non-repeated lines comparing line by line of the vector that would be filtered with the old one: p>

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 the second vector it would just give a break and leave the loop, ignoring the repetition. And if it goes through all the positions and it is different it would add such position in the new vector.

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

 if(comp1 == comp2){
    break;

}

I can not understand what I did wrong.

    
asked by anonymous 18.01.2018 / 09:49

1 answer

0

Whenever you compare strings, you must use the equals method instead of == or! =. Replace your code to have:

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

if(!comp1.equals(comp2)){
    ...
    break;
}
    
20.01.2018 / 01:19