Problem with vector comparison - Java

0

I have a problem with a simple Java code, I started right now to learn the language.

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

The problem is that it always gives me 0 in the output!

I do not know if it has to do with the cycle while() or with the increment!

Could you give me a tip? The IDE I use is NetBeans 8.1.

Below is the extract of the code responsible for this, I have a System.out.print , to print the counting of the vector that results from the join of 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);

Greetings

    
asked by anonymous 25.02.2016 / 02:23

2 answers

0

You could do different:

- Work only with the two arrays being compared, thus avoiding the creation of the third array and the two initialization loops; - In your search structure, you could remove the while (it seems that it does not fit very well according to the purpose), keep only the two "for" where one sweep listsA and the other one listB, all from position 0, because it is possible to compare them separately;

- In the if of comparison, would not need an else, because if the elements are different, there is nothing to do and the search continues freely; if they are the same, then you can save both the positions of the arrays that are repeated (create the variables before entering the search) and increase the "rep"; - at the end, you will have the last number that was repeated (accessing listA or listB by the saved position), and how many times repetitions occurred.

    
25.02.2016 / 04:20
0

Try 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);
    
25.02.2016 / 12:06