Skip to content

Instantly share code, notes, and snippets.

@ProgDan
Created April 3, 2011 16:54
Show Gist options
  • Select an option

  • Save ProgDan/900561 to your computer and use it in GitHub Desktop.

Select an option

Save ProgDan/900561 to your computer and use it in GitHub Desktop.
Resolução do exercício de vetores em linguagem Java
import java.util.Scanner;
public class LerVetores {
private int vetA[];
private int vetB[];
private Scanner ent;
/**
* Inicializa a classe de leitura de dois vetores
* @param sizeA Tamanho do primeiro vetor
* @param sizeB Tamanho do segundo vetor
*/
LerVetores(int sizeA, int sizeB) {
// Inicializa a entrada de dados
ent = new Scanner(System.in);
// Inicializa os vetores
vetA = new int[sizeA];
vetB = new int[sizeB];
}
/**
* Carrega os valores no vetor A, interagindo com o usuário
*/
public void loadVetA() {
for (int i = 0; i < vetA.length; i++) {
System.out.print("Digite o " + (i + 1) + "º valor do primeiro vetor: ");
vetA[i] = ent.nextInt();
}
}
/**
* Carrega os valores no vetor B, interagindo com o usuário
*/
public void loadVetB() {
for (int i = 0; i < vetB.length; i++) {
System.out.print("Digite o " + (i + 1) + "º valor do segundo vetor: ");
vetB[i] = ent.nextInt();
}
}
/*
* Exibe o vetor A, usando "for avançado"
*/
public void showVetA() {
for (int element : vetA) {
System.out.printf("%d\t", element);
}
System.out.println();
}
/**
* Exibe o vetor A, usando "for avançado"
*/
public void showVetB() {
for (int element : vetB) {
System.out.printf("%d\t", element);
}
System.out.println();
}
/**
* Retorna o primeiro vetor
* @return
*/
public int[] getVetA() {
return vetA;
}
/**
* Retorna o segundo vetor
* @return
*/
public int[] getVetB() {
return vetB;
}
}
public class TesteVetores {
public static void main(String[] args) {
// Inicializa a classe de leitura dos vetores
LerVetores v = new LerVetores(10, 7);
// Carrega os dados no primeiro vetor
v.loadVetA();
// Carrega os dados no segundo vetor
v.loadVetB();
// Exibe o primeiro vetor
System.out.println("Primeiro vetor:");
v.showVetA();
System.out.println();
// Exibe o segundo vetor
System.out.println("Segundo vetor:");
v.showVetB();
System.out.println();
UnirVetores un = new UnirVetores();
un.addVetor(v.getVetA());
un.addVetor(v.getVetB());
// Exibe o vetor de união
System.out.println("Vetor de União:");
un.showUnion();
}
}
public class UnirVetores {
private int union[];
/**
* Inicializa a classe de união de vetores
*/
public UnirVetores() {
// Inicializa o vetor de união como sendo NULL
union = null;
}
/**
* Adiciona um novo vetor ao vetor de união
* @param v Vetor a ser unido
*/
public void addVetor(int v[]) {
int newSize = 0;
int aux[] = new int[v.length];
for (int i = 0; i < v.length; i++) {
if (!findElement(v[i]) && !findInArray(aux, i, v[i])) {
aux[newSize] = v[i];
newSize++;
}
}
addElements(aux, newSize);
}
/**
* Busca um elemento no vetor de união
* @param element Elemento a ser buscado
* @return
*/
private boolean findElement(int element) {
// Verifica se o vetor está vazio
if (union == null) {
return false;
}
// Busca o elemento no vetor
for (int i = 0; i < union.length; i++) {
if (element == union[i]) {
return true;
}
}
// Se não encontrado, retorna false
return false;
}
/**
* Busca um elemento em uma parte de um array
* @param v Array a efetuar a busca
* @param length Índice máximo a buscar o elemento
* @param element Elemento a ser buscado
* @return
*/
private boolean findInArray(int v[], int length, int element) {
// Busca o elemento no vetor fornecido, até o tamanho máximo informado
for (int i = 0; i > length; i++) {
if (element == v[i]) {
return true;
}
}
return false;
}
/**
* Adiciona os elementos de um array ao vetor de união
* @param v Array de elementos a serem inseridos no vetor de união
* @param size Quantidade de elementos a serem inseridos no vetor de união
*/
private void addElements(int v[], int size) {
int aux[];
if (union == null) {
aux = new int[size];
// Copia os dados para o vetor auxiliar
for (int i = 0; i < size; i++) {
aux[i] = v[i];
}
// Atribui o resultado ao vetor de união
union = aux;
return;
}
aux = new int[union.length + size];
// Copia os dados do vetor de união
for (int i = 0; i < union.length; i++) {
aux[i] = union[i];
}
// Inclui os novos elementos no vetor de união
for (int i = 0; i < size; i++) {
aux[i + union.length] = v[i];
}
union = aux;
}
public void showUnion() {
for (int element : union) {
System.out.printf("%d\t", element);
}
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment