Skip to content

Instantly share code, notes, and snippets.

@gmfc
Last active December 17, 2015 18:59
Show Gist options
  • Select an option

  • Save gmfc/5656955 to your computer and use it in GitHub Desktop.

Select an option

Save gmfc/5656955 to your computer and use it in GitHub Desktop.
import javax.swing.*;
class prog{
public static void main(String args[]){
int[] v1;
int[] v2;
int tamanho;
do{
tamanho = Integer.parseInt(JOptionPane.showInputDialog("Entre com o tamanho do vetor 1"));
}while(tamanho<=0);
v1 = new int[tamanho];
do{
tamanho = Integer.parseInt(JOptionPane.showInputDialog("Entre com o tamanho do vetor 2"));
}while(tamanho<=0);
v2 = new int[tamanho];
saida("Preenchendo os vetores");
saida("Vetor 1");
leVetorInteiros (v1);
saida("Preenchendo os vetores");
saida("Vetor 2");
leVetorInteiros (v2);
saida("O que vai fazer?");
}
//===========================
public static int interseccao (int[]v1, int[]v2, int[]v3){
int n1 = v1.length;
int n2 = v2.length;
int res;
int k = 0;
for(int i = 0; i<n1;i++){//percorre o vetor v1
res = buscaVetor(v2,v1[i]);
if(res != -1){
v3[k] = v1[i];
k++;
}
}
return k;
}
//==
public static int uniao(int[]v1, int[]v2, int[]v3){
int n1 = v1.length;
int n2 = v2.length;
int res;
int k = 0;
for(int i = 0; i<n1;i++){//percorre o vetor v1
res = buscaVetor(v2,v1[i]);
if(res == -1){
v3[k] = v1[i];
k++;
}
}
return k;
}
//===========================
public static void leVetorInteiros (int v[ ]){
int tam = v.length;
for(int i = 0;i < tam;i++){
v[i] = Integer.parseInt(JOptionPane.showInputDialog("Entre com o valor da posição: "+i));
}
}
//==
public static void exibeVetorInteiros (int v[ ]){
int tam = v.length;
String saida = "";
for(int i=0; i<tam;i++){
saida = saida + "vetor["+i+"] = "+v[i]+"\n";
}
saida(saida);
}
//==
public static int buscaVetor (int v[ ], int x){
int tam = v.length;
int i;
for(i = 0;i<tam;i++){
if(v[i]==x){
return i;
}
}
return -1;
}
//==
public static void saida(String saida){
JOptionPane.showMessageDialog(null,saida);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment