Advertisement
desislava_topuzakova

02. Change List

Jun 17th, 2022
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. package Lists;
  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 ChangeList_02 {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11. List<Integer> numbers = Arrays.stream(scanner.nextLine() //"1 2 3 4 5 5 5 6"
  12. .split(" ")) //["1", "2", "3", "4", "5", "5", "5", "6"]
  13. .map(Integer::parseInt) //[1, 2, 3, 4, 5, 5, 5, 6]
  14. .collect(Collectors.toList()); //{1, 2, 3, 4, 5, 5, 5, 6}
  15.  
  16. String command = scanner.nextLine();
  17.  
  18. while (!command.equals("end")) {
  19. //1. "Delete 3".split(" ") -> ["Delete", "3"]
  20. //2. "Insert 5 0".split(" ") -> ["Insert", "5", "0"]
  21. String commandName = command.split(" ")[0];
  22. int element = Integer.parseInt(command.split(" ")[1]);
  23.  
  24. if (commandName.equals("Delete")) {
  25. numbers.removeAll(Arrays.asList(element));
  26. } else if (commandName.equals("Insert")) {
  27. int position = Integer.parseInt(command.split(" ")[2]);
  28. numbers.add(position, element);
  29. }
  30. command = scanner.nextLine();
  31. }
  32.  
  33. //{3, 4, 5, 6}
  34. for (int number : numbers) {
  35. System.out.print(number + " ");
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement