Advertisement
Guest User

Untitled

a guest
Oct 26th, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. import javax.crypto.Mac;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  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.         List listItems = Arrays
  12.                 .stream(scanner.nextLine().split(" "))
  13.                 .map(Integer::parseInt)
  14.                 .collect(Collectors.toList());
  15.         String cmd = scanner.nextLine();
  16.         while (!cmd.equalsIgnoreCase("end")) {
  17.             String[] tokens = cmd.split(" ");
  18.             int element = Integer.parseInt(tokens[1]);
  19.             switch (tokens[0]) {
  20.                 case "Delete":
  21.                     //delete all elements in the array, which are equal to element
  22.                     listItems.removeAll(Collections.singleton(element));
  23.                     break;
  24.                 case "Insert":
  25.                     int position = Integer.parseInt(tokens[2]);
  26.                     if (position >= listItems.size() || position < 0) { //if position is valid index
  27.                         listItems.add(element);
  28.                     } else {
  29.                         listItems.add(position, element);
  30.                     }
  31.                     break;
  32.             }
  33.             cmd = scanner.nextLine();
  34.         }
  35.         listItems.forEach(e -> System.out.print(e + " "));
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement