Advertisement
LoraOrliGeo

P2_ChangeList_Lists_Ex

Apr 8th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. package feb20_Lists_Exercises;
  2.  
  3. import java.util.List;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class P2_ChangeList_B {
  9.     public static void main(String[] args) {
  10.         @SuppressWarnings("resource")
  11.  
  12.         Scanner sc = new Scanner(System.in);
  13.  
  14.         List<Integer> numbers = Arrays.stream(sc.nextLine().split("\\s+")).map(Integer::parseInt)
  15.                 .collect(Collectors.toList());
  16.  
  17.         String command = "";
  18.  
  19.         while (!"end".equals(command = sc.nextLine())) {
  20.             String[] input = command.split("\\s+");
  21.             String operation = input[0];
  22.             int element = Integer.parseInt(input[1]);
  23.  
  24.             switch (operation) {
  25.             case "Delete":
  26.                 numbers = numbers.stream().filter(e -> e != element).collect(Collectors.toList());
  27.                 break;
  28.             case "Insert":
  29.                 int position = Integer.parseInt(input[2]);
  30.                 if (position >= 0 && position < numbers.size()) {
  31.                     numbers.add(position, element);
  32.                 }
  33.                 break;
  34.             }
  35.         }
  36.  
  37.         System.out.println(numbers.toString().replaceAll("\\[|,|\\]", ""));
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement