Advertisement
IvaAnd

2. Change List

Jun 29th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 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. import static java.lang.Integer.valueOf;
  7.  
  8. public class Ex02_ChangeList {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         List<Integer> lineNums = Arrays.stream(scanner.nextLine().split(" "))
  13.                 .map(Integer::parseInt)
  14.                 .collect(Collectors.toList());
  15.  
  16.         String command = scanner.nextLine();
  17.  
  18.         while (!command.contains("end")) {
  19.             String[] tokens = command.split(" +");
  20.             int number = Integer.parseInt(tokens[1]);
  21.  
  22.             if (tokens[0].equals("Delete")) {
  23.                 while (lineNums.contains(number)) {
  24.                     lineNums.remove(Integer.valueOf(number));
  25.                 }
  26.  
  27.             } else if (tokens[0].equals("Insert")) {
  28.                 int index = Integer.parseInt(tokens[2]);
  29.                 lineNums.add(index, number);
  30.  
  31.             }
  32.  
  33.             command = scanner.nextLine();
  34.         }
  35.  
  36.         for (int i = 0; i < lineNums.size(); i++) {
  37.             System.out.print(lineNums.get(i) + " ");
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement