Advertisement
deyanmalinov

04. List Operations

Mar 4th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. package com.company;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4. public class Main {
  5.         public static void main(String[] args) {
  6.             Scanner scan = new Scanner(System.in);
  7.             List <String> strList = Arrays.stream(scan.nextLine().split("\\s+")).collect(Collectors.toList());
  8.             String line = scan.nextLine();
  9.  
  10.             while (!line.equals("End")){
  11.  
  12.                 String [] comand = line.split("\\s+");
  13.                 switch (comand[0]){
  14.                     case "Add":
  15.                         strList.add(comand[1]);
  16.                         break;
  17.                     case "Remove":
  18.                         if (strList.size()>Integer.parseInt(comand[1])){
  19.                             strList.remove(Integer.parseInt(comand[1]));
  20.                         }else {
  21.                             System.out.println("Invalid index");
  22.                         }
  23.                         break;
  24.                     case "Insert":
  25.                         if (strList.size()>Integer.parseInt(comand[2])) {
  26.                             strList.add(Integer.parseInt(comand[2]), comand[1]);
  27.                         }else {
  28.                             System.out.println("Invalid index");
  29.                         }
  30.                         break;
  31.                     case "Shift":
  32.                         if (comand[1].equals("left")){
  33.                             for (int i = 0; i < Integer.parseInt(comand[2]); i++) {
  34.                                 strList.add(strList.get(0));
  35.                                 strList.remove(0);
  36.                             }
  37.  
  38.                         }else {
  39.                             for (int i = 0; i < Integer.parseInt(comand[2]); i++) {
  40.                                 strList.add(0, strList.get(strList.size()-1));
  41.                                 strList.remove(strList.size()-1);
  42.                             }
  43.                         }
  44.  
  45.                         break;
  46.  
  47.                 }
  48.                 line = scan.nextLine();
  49.             }
  50.             for (String s : strList) {
  51.                 System.out.print(s+" ");
  52.             }
  53.  
  54.         }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement