Advertisement
petur_stoqnov

ListOperations

Feb 20th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class ListOperations {
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8. String[] array = sc.nextLine().split("\\s+");
  9. List<Integer> list = new ArrayList<>();
  10.  
  11. for (int i = 0; i < array.length; i++) {
  12. list.add(Integer.parseInt(array[i]));
  13. }
  14.  
  15. String[] command = sc.nextLine().split("\\s+");
  16. while (!command[0].equals("End")) {
  17. switch (command[0]) {
  18. case "Add":
  19. int number1 = Integer.parseInt(command[1]);
  20. list.add(number1);
  21. break;
  22. case "Insert":
  23. int index = Integer.parseInt(command[2]);
  24. int number2 = Integer.parseInt(command[1]);
  25. if (index < list.size() && index >= 0) {
  26. list.add(index, number2);
  27. } else {
  28. System.out.println("Invalid index");
  29. }
  30. break;
  31. case "Remove":
  32. int index2 = Integer.parseInt(command[1]);
  33. if (index2 < list.size() && index2 >= 0) {
  34. list.remove(index2);
  35. } else {
  36. System.out.println("Invalid index");
  37. }
  38. break;
  39. case "Shift":
  40. String position = command[1];
  41. int count = Integer.parseInt(command[2]);
  42. if (position.equalsIgnoreCase("left")) {
  43. for (int i = 1; i <= count; i++) {
  44. int firstElement = list.get(0);
  45. for (int j = 0; j < list.size() - 1; j++) {
  46. int element = list.get(j + 1);
  47. list.set(j, element);
  48. }
  49. list.set(list.size() - 1, firstElement);
  50. }
  51. } else if (position.equals("right")) {
  52. for (int i = 1; i <= count; i++) {
  53. int lastElement = list.get(list.size() - 1);
  54. for (int j = list.size() - 1; j > 0; j--) {
  55. int element = list.get(j - 1);
  56. list.set(j, element);
  57. }
  58. list.set(0, lastElement);
  59. }
  60. }
  61. break;
  62. }
  63. command = sc.nextLine().split("\\s+");
  64. }
  65. for (int print : list) {
  66. System.out.print(print + " ");
  67. }
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement