Didart

Change List

Oct 15th, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class ChangeList {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<Integer> numbers = Arrays
  11.                 .stream(scanner.nextLine().split(" "))
  12.                 .map(Integer::parseInt)
  13.                 .collect(Collectors.toList());
  14.  
  15.         String command = scanner.nextLine();
  16.  
  17.         while (!command.equals("end")) {
  18.  
  19.             String commandName = command.split(" ")[0];
  20.             int element = Integer.parseInt(command.split(" ")[1]);
  21.  
  22.             if (commandName.equals("Delete")) {
  23.                 numbers.removeAll(Arrays.asList(element));
  24.             } else if (commandName.equals("Insert")) {
  25.                 int position = Integer.parseInt(command.split(" ")[2]);
  26.                 numbers.add(position, element);
  27.             }
  28.  
  29.             command = scanner.nextLine();
  30.         }
  31.  
  32.         for (int number : numbers) {
  33.             System.out.print(number + " ");
  34.         }
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment