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