Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- public class Pet_Pet_A {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int[] arguments = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::valueOf).toArray();
- List<Integer> array = new ArrayList<Integer>();
- for (int i = 0; i < arguments.length; i++)
- array.add(arguments[i]);
- String[] input = sc.nextLine().split(" ");
- while (!input[0].equals("print")) {
- switch (input[0]) {
- case "push":
- array = push(array, input);
- break;
- case "shift":
- array = shift(array);
- break;
- case "addMany":
- array = addMany(array, input);
- break;
- case "pop":
- array = pop(array);
- break;
- case "remove":
- array = remove(array, input);
- break;
- }
- input = sc.nextLine().split(" ");
- }
- print(array);
- }
- public static List<Integer> push(List<Integer> array, String[] input) {
- List<Integer> temp_array = array;
- temp_array.add((Integer.parseInt(input[1])));
- return temp_array;
- }
- public static List<Integer> shift(List<Integer> array) {
- List<Integer> temp_array = array;
- int temp = temp_array.get(0);
- temp_array.set(0, temp_array.get(temp_array.size() - 1));
- temp_array.set(temp_array.size() - 1, temp);
- return temp_array;
- }
- public static List<Integer> addMany(List<Integer> array, String[] input) {
- List<Integer> temp_array = array;
- for (int i = input.length - 2; i > 0; i--) {
- temp_array.add(Integer.parseInt(input[1]), Integer.parseInt(input[i + 1]));
- }
- return temp_array;
- }
- public static List<Integer> pop(List<Integer> array) {
- List<Integer> temp_array = array;
- System.out.println(temp_array.get((temp_array.size() - 1)));
- temp_array.remove(temp_array.size() - 1);
- return temp_array;
- }
- public static List<Integer> remove(List<Integer> array, String[] input) {
- List<Integer> temp_array = array;
- if (Integer.parseInt(input[1]) <= temp_array.size() - 1) {
- temp_array.remove(Integer.parseInt(input[1]));
- }
- return temp_array;
- }
- public static void print(List<Integer> array) {
- array = reverse(array);
- for (int i = array.size() - 1; i > 0; i--) {
- System.out.print(array.get(i) + ", ");
- }
- System.out.print(array.get(0));
- }
- public static List<Integer> reverse(List<Integer> a) {
- Collections.reverse(Arrays.asList(a));
- return a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement