Vector Comparison Problem - Java

Asked

Viewed 510 times

0

I have a problem with a simple code in Java, I just started learning the language.

The exercise consists of comparing the values of the vector to see if there are repeated elements.

The problem is, it always gives me 0 in output!

I don’t know if it has to do with the cycle while() or by increasing!

Could you give me a hint ? The IDE I use is Netbeans 8.1.

Below is the extract of the code responsible for that, I have a System.out.print, to print the count of the vector resulting from the junction of the listaA and listaB:

    int[] listaA = {2, -5, -121, 102, -35, -2, 0, -125, 802, -10};
    int[] listaB = {6, 99, -1, 12, 1, -2};
    int[] novoVetor;

    novoVetor = new int[listaA.length + listaB.length];  // tamanha do vetor A e o tamanho do vetor B
    int nr_vetorB = listaA.length, rep=0, g=0;

    for (int i = 0; i < listaA.length; i++) {
        novoVetor[i] = listaA[i];
    }
    for (int j = 0; j < listaB.length; j++) {
        novoVetor[nr_vetorB] = listaB[j];
        nr_vetorB++;
    }


    for (int x = 0; x < novoVetor.length; x++) {
        while( g < novoVetor.length) {
            for (int y = 1; y < novoVetor.length; y++) {
                if (novoVetor[x] == novoVetor[y]) {
                    g = novoVetor.length;
                    rep++;
                } else {
                    g++;
                }


            }

        }
        System.out.print(" " +novoVetor[x]);    
        }

        System.out.print("\nElementos repetidos:" +rep);

Regards

2 answers

0

You could do it differently:

- Work only with the two arrays being compared, thus avoiding the creation of the third array and the two boot loops;

- In its search structure, it could remove the while (it seems that it does not fit very well according to the purpose), keep only the two "for" where one scan lists A and the other list B, all from position 0, as it is possible to compare them separately;

- In the case of comparison, it would not need an Else, because if the elements are different, there is nothing to do and the search continues freely; if they are equal, then Voce can store both the positions of the arrays that are repeated (create the variables before entering the search) how much to increase the "rep";

- at the end, Voce will have the last number that was repeated (accessing listA or listB by the saved position), and how many times there were repetitions.

  • If you need the single array, then I think the points to be reviewed are just the while, which in my opinion is not necessary, and the Else which is also of no use.

0

Try it like this:

    int[] listaA = {2, -5, -121, 102, -35, -2, 0, -125, 802, -10};
    int[] listaB = {6, 99, -1, 12, 1, -2};
    int[] todos = new int[listaA.length+listaB.length];

    int pt = 0;
    for(int i : listaA){
        todos[pt] = i;
        pt++;
    }
    for(int i : listaB){
        todos[pt] = i;
        pt++;
    }

    int total = 0;
    for(int ptA=0; ptA < todos.length; ptA++){
        for(int ptB=0; ptB < todos.length; ptB++){
            if(ptA == ptB){ // se os ponteiros forem iguais desconsidera..
                continue;
            }
            if(todos[ptA] == todos[ptB]){
                total++;
            }
        }
    }

    total = (total/2); /// pois irá comparar o mesmo item 2 vezes..
    System.out.println("Elementos repetidos: "+total);
  • thank you, in the exercise I really have to create a vector with the sum of the two and make the comparison in this same vector! thank you again :)

  • 1

    All you have to do is count how many repeat? When there are rules requested, it is interesting to inform in the question, so it makes it easier to help!

Browser other questions tagged

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