Advertisement
vencinachev

numCommands

Mar 18th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class NumCmd {
  5.  
  6.     public static void main(String[] args) {
  7.        
  8.         Scanner scan = new Scanner(System.in);
  9.         ArrayList<Integer> numbers = new ArrayList<Integer>();
  10.        
  11.         String[] input = scan.nextLine().split(" ");
  12.         for (int i = 0; i < input.length; i++) {
  13.             numbers.add(Integer.parseInt(input[i]));
  14.         }
  15.        
  16.         while (true) {
  17.             String[] commands = scan.nextLine().split(" ");
  18.             if (commands[0].equals("print")) {
  19.                 break;
  20.             } else if (commands[0].equals("push")) {
  21.                 int num = Integer.parseInt(commands[1]);
  22.                 numbers.add(num);
  23.             } else if (commands[0].equals("pop")) {
  24.                 System.out.println(numbers.get(numbers.size() - 1));
  25.                 numbers.remove(numbers.size() - 1);
  26.             } else if (commands[0].equals("shift")) {
  27.                 int temp = numbers.get(0);
  28.                 numbers.set(0, numbers.get(numbers.size() - 1));
  29.                 numbers.set(numbers.size() - 1, temp);
  30.             }
  31.         }
  32.        
  33.         for (int i = numbers.size() - 1; i >= 0; i--) {
  34.             System.out.print(numbers.get(i) + " ");
  35.         }
  36.        
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement