Advertisement
desislava_topuzakova

04. List Operations

Jun 9th, 2023
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 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.  
  12. List<Integer> numbers = Arrays.stream(scanner.nextLine() //"4 5 6 7 2 3"
  13. .split(" "))//["4", "5", "6", "7", "2", "3"]
  14. .map(Integer::parseInt)// [4, 5, 6, 7, 2, 3]
  15. .collect(Collectors.toList());// {4, 5, 6, 7, 2, 3}
  16.  
  17. String command = scanner.nextLine();
  18.  
  19. while (!command.equals("End")) {
  20. String[] commandParts = command.split(" ");
  21. String commandName = commandParts[0]; //"Add", "Insert", "Remove", "Shift"
  22. switch (commandName) {
  23. case "Add":
  24. //command = "Add {number}".split(" ") -> ["Add", "{number}"]
  25. int numberToAdd = Integer.parseInt(commandParts[1]);
  26. numbers.add(numberToAdd);
  27. break;
  28. case "Insert":
  29. //command = "Insert {number} {index}".split(" ") -> ["Insert", "{number}", "{index}"]
  30. int numberForInsert = Integer.parseInt(commandParts[1]);
  31. int indexForInsert = Integer.parseInt(commandParts[2]);
  32. //проверка дали дадения индекс е валиден -> индекс е [0; size - 1]
  33. if (isValidIndex(indexForInsert, numbers.size())) {
  34. numbers.add(indexForInsert, numberForInsert);
  35. } else {
  36. //невалиден индекс
  37. System.out.println("Invalid index");
  38. }
  39. break;
  40. case "Remove":
  41. //command = "Remove {index}".split(" ") -> ["Remove", "{index}"]
  42. int indexToRemove = Integer.parseInt(commandParts[1]);
  43. //проверка дали дадения индекс е валиден -> индекс е [0; size - 1]
  44. if (isValidIndex(indexToRemove, numbers.size())) {
  45. numbers.remove(indexToRemove);
  46. //remove index -> numbers.remove((int) index);
  47. //remove element -> numbers.remove(Integer.valueOf(number));
  48. } else {
  49. //невалиден индекс
  50. System.out.println("Invalid index");
  51. }
  52. break;
  53. case "Shift":
  54. String direction = commandParts[1]; //посока: "left" или "right"
  55. int countTimes = Integer.parseInt(commandParts[2]); //брой на пътите, в които правите промяна
  56. if (direction.equals("left")) {
  57. //command = "Shift left {count}".split(" ") -> ["Shift", "left", "{count}"]
  58. for (int i = 1; i <= countTimes; i++) {
  59. //повтаряме: първия елемент става последен
  60. //{3, 4, 5, 7, 8}
  61. //1. взимам първия елемент -> 3
  62. int firstElement = numbers.get(0);
  63. //2. премахвам го -> {4, 5, 7, 8}
  64. numbers.remove(0);
  65. //3. поставям го като последен -> {4, 5, 7, 8, 3}
  66. numbers.add(firstElement);
  67. }
  68. } else if (direction.equals("right")) {
  69. //command = "Shift right {count}".split(" ") -> ["Shift", "right", "{count}"]
  70. for (int i = 1; i <= countTimes; i++) {
  71. //повтаряме: последния елемент става първи
  72. //{8, 4, 5, 6, 2}
  73. //1. взимаме последния елемент -> 2
  74. int lastElement = numbers.get(numbers.size() - 1);
  75. //2. премахваме последния елемент -> {8, 4, 5, 6}
  76. numbers.remove(numbers.size() - 1);
  77. //3. добавяме в началото -> {2, 8, 4, 5, 6}
  78. numbers.add(0, lastElement);
  79. }
  80. }
  81. break;
  82. }
  83. command = scanner.nextLine();
  84. }
  85.  
  86. //краен списък -> отпечатвам
  87. for (int number : numbers) {
  88. System.out.print(number + " ");
  89. }
  90.  
  91. }
  92.  
  93. //метод, който проверява дали даден индекс е валиден
  94. //true -> ако индексът е валиден
  95. //false -> ако индексът не е валиден
  96. public static boolean isValidIndex (int index, int size) {
  97. return index >= 0 && index <= size - 1;
  98. }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement