Advertisement
Guest User

ChangeList

a guest
Feb 16th, 2020
202
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.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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement