… pooo
O que não entendeu?
Alguém pode me ajudar em uma questão com vetores ??? :`)
Qual a duvida, posta o codigo, o enunciado da questão
Se não postar sua dúvida, ninguém vai saber como ajudar.
Eu consegui fazer a intercessão, mas a união não
A união é quando os numero de vetor A1 e A2 são iguais
public class Questao18{
public static void main(String[] args) {
int [] vetorA = new int[3];
int [] vetorB = new int[3];
vetorA[0] = 5;
vetorA[1] = 6;
vetorA[2] = 7;
vetorB[0] = 5;
vetorB[1] = 4;
vetorB[2] = 8;
int tamanho = 0;
int fixo = 0;
int cont = 0;
for ( fixo = 0; fixo < vetorA.length; fixo++) {
for(cont = 0; cont < vetorB.length; cont++) {
if (vetorA[fixo] == vetorB[cont]) {
tamanho++;
}
}
}
int [] vetorC = new int [tamanho];
tamanho = 0;
for (fixo = 0; fixo < vetorA.length; fixo++) {
for(cont = 0; cont < vetorB.length; cont++) {
if (vetorA[fixo] == vetorB[cont]) {
vetorC [tamanho] = vetorB[cont];
System.out.println(vetorC[tamanho] + " ");
tamanho++;
}
}
}
}
}
assim estaria certo?
Não, a união é um vetor que contém todos os elementos do 1º e todos os elementos do 2º vetor.
Exemplo:
A1 = 1, 2, 3, 4, 5
A2 = 3, 4, 5, 6, 7
A1 + A2 = 1, 2, 3, 4, 5, 6, 7
como eu poderia resolve - la ?
alterando esse código
Assim… vlw @staroski.
Acho que vc terá que instanciar um novo vetor para armazenar a união.
int uniao[] = new int[a1.length + b.length]; // aqui vc determina o tamanho do vetor, conforme o
//tamanho dos vetores que possuem valor
Então é so percorrer os vetores a1, e add no vetor uniao, depois percorre o vetor a2 e add no vetor uniao
ah, então se no vetor A1 possuir o numero no vetor A2, ou seja (a1 == a2) o tamanho do vetor aux;. será diminuido?
import java.util.Scanner;
public class Questao18{
public static void main(String[] args) {
Scanner leia = new Scanner (System.in);
int [] vetorA = new int[3];
int [] vetorB = new int[3];
for (int i = 0; i < vetorB.length; i++) {
vetorA[i] = leia.nextInt();
}
for (int i = 0; i < vetorB.length; i++) {
vetorB[i] = leia.nextInt();
}
int tamanho = 2 * vetorA.length;
for (int i = 0; i < vetorB.length; i++) {
for (int j = 0; j < vetorB.length; j++) {
if (vetorA[i] == vetorB[j]) {
tamanho --;
}
}
}
int [] vetor = new int [tamanho];
for (int i = 0; i < vetorA.length; i++) {
vetor[i] = vetorA[i];
}
tamanho = vetorA.length + 1;
for (int i = 0; i < vetor.length; i++) {
for (int j = 0; j < vetor.length; j++) {
if (vetor[i] != vetorB[j]) {
vetor[tamanho] = vetorB[j];
tamanho++;
}
}
}
for (int i = 0; i < vetor.length; i++) {
System.out.println(vetor[i] + " ");
}
}
}
// ainda continua dando erro na linha 39 , aff