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
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.
– Ismael Fofonka dos Santos