Advertisement
desislava_topuzakova

Untitled

Jun 17th, 2022
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 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 ListOperations_04 {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11. List<Integer> numbers = Arrays.stream(scanner.nextLine() //"1 23 29 18 43 21 20"
  12. .split(" ")) // ["1", "23", "29", "18", "43", "21", "20"]
  13. .map(Integer::parseInt)// [1, 23, 29, 18, 43, 21, 20]
  14. .collect(Collectors.toList()); // {1, 23, 29, 18, 43, 21, 20}
  15.  
  16. String command = scanner.nextLine();
  17. while (!command.equals("End")) {
  18. if (command.contains("Add")) {
  19. //command = "Add 4".split(" ") -> ["Add", "4"]
  20. int numberToAdd = Integer.parseInt(command.split(" ")[1]);
  21. numbers.add(numberToAdd);
  22. } else if (command.contains("Insert")) {
  23. //command = "Insert 4 0".split(" ") -> ["Insert", "4", "0"]
  24. int numberToInsert = Integer.parseInt(command.split(" ")[1]);
  25. int index = Integer.parseInt(command.split(" ")[2]);
  26. //проверка за индекса
  27. if (isIndexValid(index, numbers.size())) {
  28. //валиден (0 до дълж - 1) -> добавяме елемента на дадения индекс
  29. numbers.add(index, numberToInsert);
  30. } else {
  31. //невалиден -> "Invalid index"
  32. System.out.println("Invalid index");
  33. }
  34. } else if (command.contains("Remove")) {
  35. //command = "Remove 1".split(" ") -> ["Remove", "1"]
  36. int indexToRemove = Integer.parseInt(command.split(" ")[1]);
  37. //проверка за индекса
  38. if (isIndexValid(indexToRemove, numbers.size())) {
  39. //валиден -> премахваме елемента на дадения индекс
  40. numbers.remove(indexToRemove);
  41. } else {
  42. //невалиден -> "Invalid index"
  43. System.out.println("Invalid index");
  44. }
  45. } else if (command.contains("Shift left")) {
  46. //command = "Shift left 2".split(" ") -> ["Shift", "left", "2"]
  47. int countLeft = Integer.parseInt(command.split(" ")[2]);
  48. //поватаряме countLeft на брой пъти
  49. for (int time = 1; time <= countLeft; time++) {
  50. //first number becomes last
  51. //{3, 4, 6, 7, 1}
  52. //firstNumber = 3 -> index на lastNumber (дължина - 1)
  53. int firstNumber = numbers.get(0);
  54. numbers.add(firstNumber); //{3, 4, 6, 7, 1, 3}
  55. numbers.remove(0); //{3, 4, 6, 7, 1, 3}
  56. }
  57. } else if (command.contains("Shift right")) {
  58. //command = "Shift right 3".split(" ") -> ["Shift", "right", "3"]
  59. int countRight = Integer.parseInt(command.split(" ")[2]);
  60. //поватаряме countRight на брой пъти
  61. for (int time = 1; time <= countRight; time++) {
  62. //last number becomes first
  63. //{3, 4, 6, 7, 1}
  64. //lastNumber = 1 -> index = 0
  65. int lastNumber = numbers.get(numbers.size() - 1); //1
  66. numbers.add(0, lastNumber); //{1, 3, 4, 6, 7, 1}
  67. numbers.remove(numbers.size() - 1); //{1, 3, 4, 6, 7}
  68. }
  69. }
  70. command = scanner.nextLine();
  71. }
  72.  
  73. for (int number : numbers) {
  74. System.out.print(number + " ");
  75. }
  76. }
  77.  
  78. //метод, който валидира индекса
  79. //true -> валиден индекс
  80. //false -> невалиден индекс
  81. public static boolean isIndexValid (int index, int sizeOfList) {
  82. //[0 до последния]
  83. return index >= 0 && index <= sizeOfList - 1;
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement