Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. package Lists;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class ChangeList {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         List<Long> numbers = Arrays.stream(scanner.nextLine().split(" "))
  13.                 .map(Long::parseLong).collect(Collectors.toList());
  14.  
  15.         String input = scanner.nextLine();
  16.  
  17.         while (!"end".equalsIgnoreCase(input)) {
  18.  
  19.             String[] command = input.split(" ");
  20.  
  21.             if ("Delete".equalsIgnoreCase(command[0])) {
  22.  
  23.                 long nummberToDelete = Long.parseLong(command[1]);
  24.  
  25.                 numbers.removeIf(n ->  n == nummberToDelete);
  26.  
  27.             } else if ("Insert".equalsIgnoreCase(command[0])) {
  28.  
  29.                 long numberToInsert = Long.parseLong(command[1]);
  30.                 int indexToInsert = Integer.parseInt(command[2]);
  31.  
  32.                 if(indexToInsert < numbers.size() && indexToInsert >= 0) {
  33.  
  34.                     numbers.add(indexToInsert, numberToInsert);
  35.                 }
  36.             }
  37.  
  38.             input = scanner.nextLine();
  39.         }
  40.  
  41.         System.out.println(numbers.toString().replaceAll("[\\[,\\]]",""));
  42.  
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement