Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5. import java.util.Comparator;
  6. import java.util.List;
  7. import java.util.stream.Collectors;
  8.  
  9. class Main {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.         List<String> inputWords = Arrays.stream(read.readLine().split("\\s+")).collect(Collectors.toList());
  14.  
  15.         String inputCom;
  16.  
  17.  
  18.         while (!"Stop".equals(inputCom = read.readLine())) {
  19.             String[] com = inputCom.split("\\s+");
  20.  
  21.             switch (com[0]) {
  22.                 case "Delete":
  23.                     int index = Integer.parseInt(com[1]) + 1;
  24.                     if (index >= 0 && index <= inputWords.size() - 1) {
  25.                         inputWords.remove(index);
  26.                     }
  27.                     break;
  28.                 case "Swap":
  29.  
  30.                     if (inputWords.contains(com[1]) && inputWords.contains(com[2])) {
  31.  
  32.                         int indexFirst = inputWords.indexOf(com[1]);
  33.                         int indexSecond = inputWords.indexOf(com[2]);
  34.  
  35.                         String firstWord = inputWords.get(indexFirst);
  36.                         String secondWord = inputWords.get(indexSecond);
  37.                         inputWords.set(indexSecond, firstWord);
  38.                         inputWords.set(indexFirst, secondWord);
  39.                     }
  40.                     break;
  41.                 case "Put":
  42.  
  43.                     int indexWord = inputWords.indexOf(com[1]);
  44.                     int indexPut = Integer.parseInt(com[2]) - 1;
  45.  
  46.                     if (indexWord != -1) {
  47.                         inputWords.add(indexWord - 1, com[1]);
  48.                     } else if (indexPut >= 0 && indexPut < inputWords.size()) {
  49.                         inputWords.add(indexPut, com[1]);
  50.                     }
  51.                     break;
  52.                 case "Sort":
  53.                     inputWords = inputWords.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
  54.                     break;
  55.                 case "Replace":
  56.  
  57.                     if (inputWords.contains(com[2])) {
  58.                         int indexSecondWord = inputWords.indexOf(com[2]);
  59.                         inputWords.set(indexSecondWord, com[1]);
  60.                     }
  61.                     break;
  62.             }
  63.         }
  64.         inputWords.forEach(e -> System.out.print(e + " "));
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement