SHARE
TWEET

ChangeList

a guest Feb 16th, 2020 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class ChangeList {
  8.     public static void main(String[] args) {
  9.         Scanner scan = new Scanner(System.in);
  10.  
  11.         String input = scan.nextLine();
  12.         List<String> myList = Arrays.stream(input.split("\\s+"))
  13.                 .collect(Collectors.toList());
  14.  
  15.         String command = scan.nextLine();
  16.  
  17.         while (!"end".equals(command)) {
  18.             String[] deleteOrInsert = command.split("\\s+");
  19.             if ("Delete".equals(deleteOrInsert[0])) {
  20.                 while (myList.contains(deleteOrInsert[1])) {
  21.                     myList.remove(deleteOrInsert[1]);
  22.                 }
  23.  
  24.  
  25.             } else if ("Insert".equals(deleteOrInsert[0])) {
  26.  
  27.                 int index = Integer.parseInt(deleteOrInsert[2]);
  28.                 if (index <= myList.size()) {
  29.  
  30.                     myList.add(index, deleteOrInsert[1]);
  31.                 }
  32.  
  33.  
  34.             }
  35.  
  36.             command = scan.nextLine();
  37.         }
  38.         for (String element : myList) {
  39.             System.out.print(element + " ");
  40.  
  41.         }
  42.  
  43.     }
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top