Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class ListOperations {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String[] array = sc.nextLine().split("\\s+");
- List<Integer> list = new ArrayList<>();
- for (int i = 0; i < array.length; i++) {
- list.add(Integer.parseInt(array[i]));
- }
- String[] command = sc.nextLine().split("\\s+");
- while (!command[0].equals("End")) {
- switch (command[0]) {
- case "Add":
- int number1 = Integer.parseInt(command[1]);
- list.add(number1);
- break;
- case "Insert":
- int index = Integer.parseInt(command[2]);
- int number2 = Integer.parseInt(command[1]);
- if (index < list.size() && index >= 0) {
- list.add(index, number2);
- } else {
- System.out.println("Invalid index");
- }
- break;
- case "Remove":
- int index2 = Integer.parseInt(command[1]);
- if (index2 < list.size() && index2 >= 0) {
- list.remove(index2);
- } else {
- System.out.println("Invalid index");
- }
- break;
- case "Shift":
- String position = command[1];
- int count = Integer.parseInt(command[2]);
- if (position.equalsIgnoreCase("left")) {
- for (int i = 1; i <= count; i++) {
- int firstElement = list.get(0);
- for (int j = 0; j < list.size() - 1; j++) {
- int element = list.get(j + 1);
- list.set(j, element);
- }
- list.set(list.size() - 1, firstElement);
- }
- } else if (position.equals("right")) {
- for (int i = 1; i <= count; i++) {
- int lastElement = list.get(list.size() - 1);
- for (int j = list.size() - 1; j > 0; j--) {
- int element = list.get(j - 1);
- list.set(j, element);
- }
- list.set(0, lastElement);
- }
- }
- break;
- }
- command = sc.nextLine().split("\\s+");
- }
- for (int print : list) {
- System.out.print(print + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement