Advertisement
veronikaaa86

04. List Manipulation Basics

Jun 15th, 2022
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. package list;
  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 P04ListManipulationBasics {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. List<Integer> numList = Arrays.stream(scanner.nextLine().split(" "))
  13. .map(Integer::parseInt)
  14. .collect(Collectors.toList());
  15.  
  16. String input = scanner.nextLine();
  17. while (!input.equals("end")) {
  18. List<String> commandLine = Arrays.stream(input.split(" ")).collect(Collectors.toList());
  19. String command = commandLine.get(0);
  20.  
  21. int item = 0;
  22. int index = 0;
  23. switch (command) {
  24. case "Add":
  25. item = Integer.parseInt(commandLine.get(1));
  26. numList.add(item);
  27. break;
  28. case "Remove":
  29. item = Integer.parseInt(commandLine.get(1));
  30. numList.remove(Integer.valueOf(item));
  31. break;
  32. case "RemoveAt":
  33. index = Integer.parseInt(commandLine.get(1));
  34. numList.remove(index);
  35. break;
  36. case "Insert":
  37. item = Integer.parseInt(commandLine.get(1));
  38. index = Integer.parseInt(commandLine.get(2));
  39. numList.add(index, item);
  40. break;
  41. }
  42.  
  43. input = scanner.nextLine();
  44. }
  45.  
  46. System.out.println(numList.toString().replaceAll("[\\[\\],]", ""));
  47. }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement