LoraOrliGeo

ListManipulationBasics_Lists_Lab

Apr 8th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. package feb19_Lists_Lab;
  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 ListManipulationBasics {
  9.     public static void main(String[] args) {
  10.         @SuppressWarnings("resource")
  11.  
  12.         Scanner sc = new Scanner(System.in);
  13.  
  14.         List<Integer> input = Arrays.stream(sc.nextLine().split("\\s+")).map(Integer::parseInt)
  15.                 .collect(Collectors.toList());
  16.  
  17.         String command = "";
  18.  
  19.         while (!"end".equals(command = sc.nextLine())) {
  20.             String[] toDo = command.split("\\s+");
  21.             String operation = toDo[0];
  22.             int value = Integer.parseInt(toDo[1]);
  23.  
  24.             switch (operation) {
  25.             case "Add":
  26.                 input.add(value);
  27.                 break;
  28.             case "Remove":
  29.                 input.remove(Integer.valueOf(value));
  30.                 break;
  31.             case "RemoveAt":
  32.                 input.remove(value);
  33.                 break;
  34.             case "Insert":
  35.                 int index = Integer.parseInt(toDo[2]);
  36.                 input.add(index, value);
  37.                 break;
  38.             }
  39.         }
  40.        
  41.         System.out.println(input.toString().replaceAll("\\[|,|\\]", ""));
  42.     }
  43. }
Add Comment
Please, Sign In to add comment