Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Stack;
- import java.util.Random;
- public class TP2E03 {
- public static void main(String[] args) {
- TP2E03 excercise = new TP2E03();
- excercise.execute();
- }
- private void execute() {
- Stack<Integer> myStack1 = new Stack<Integer>();
- Stack<Integer> myStack2 = new Stack<Integer>();
- Random random = new Random();
- for (int max = random.nextInt(10) + 1; max > 0; max--) {
- myStack1.push(random.nextInt(100));
- }
- for (int max = random.nextInt(10) + 1; max > 0; max--) {
- myStack2.push(random.nextInt(100));
- }
- System.out.println("Pila 1 " + myStack1.toString());
- System.out.println("Pila 2 " + myStack2.toString());
- System.out.println("Union " + Union(myStack1, myStack2));
- System.out.println("Pila 1 " + myStack1.toString());
- System.out.println("Pila 2 " + myStack2.toString());
- }
- private Stack<Integer> Union(Stack<Integer> stack1, Stack<Integer> stack2) {
- Stack<Integer> result = new Stack<Integer>();
- Integer[] data = new Integer[stack1.size()];
- stack1.toArray(data);
- for (int i = 0; i < data.length; i++) {
- result.push(data[i]);
- }
- data = new Integer[stack2.size()];
- stack2.toArray(data);
- for (int i = 0; i < data.length; i++) {
- result.push(data[i]);
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement