Created
August 10, 2014 00:49
-
-
Save caiquecastro/00b04201cc304f6765f9 to your computer and use it in GitHub Desktop.
União de Conjuntos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
import java.util.TreeSet; | |
import java.util.Scanner; | |
public class Ex3 { | |
public static void main(String[] args) { | |
int valor, i; | |
Scanner in = new Scanner(System.in); | |
TreeSet<Integer> conjuntoA = new TreeSet<Integer>(), | |
conjuntoB = new TreeSet<Integer>(); | |
boolean first = true; | |
System.out.println("Digite os elementos do conjunto A:"); | |
do { | |
System.out.print("Digite um valor: "); | |
valor = in.nextInt(); | |
if(valor > 0) | |
conjuntoA.add(valor); | |
} while(valor > 0); | |
System.out.println("Digite os elementos do conjunto B:"); | |
do { | |
System.out.print("Digite um valor: "); | |
valor = in.nextInt(); | |
if(valor > 0) | |
conjuntoB.add(valor); | |
} while(valor > 0); | |
Iterator<Integer> iteratorA = conjuntoA.iterator(), | |
iteratorB = conjuntoB.iterator(); | |
System.out.println("Conjunto A:"); | |
System.out.print("{ "); | |
while (iteratorA.hasNext()) { | |
if(first) { | |
System.out.print(iteratorA.next()); | |
} else { | |
System.out.print(", " + iteratorA.next()); | |
} | |
first = false; | |
} | |
System.out.println(" }\n"); | |
first = true; | |
System.out.println("Conjunto B:"); | |
System.out.print("{ "); | |
while (iteratorB.hasNext()) { | |
if(first) { | |
System.out.print(iteratorB.next()); | |
} else { | |
System.out.print(", " + iteratorB.next()); | |
} | |
first = false; | |
} | |
System.out.println(" }\n"); | |
conjuntoA.addAll(conjuntoB); | |
first = true; | |
System.out.println("União de A com B"); | |
System.out.print("{ "); | |
iteratorA = conjuntoA.iterator(); | |
while(iteratorA.hasNext()) { | |
if(first) { | |
System.out.print(iteratorA.next()); | |
} else { | |
System.out.print(", " + iteratorA.next()); | |
} | |
first = false; | |
} | |
System.out.println(" }\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment