Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11. String[] strings=scanner.nextLine().split("\\s+");
  12. List<String> list=new ArrayList<>();
  13. for (String string:strings) {
  14. list.add(string);
  15. }
  16. String[] command=scanner.nextLine().split("\\s+");
  17. while (!"Stop".equals(command[0])) {
  18. if("Delete".equals(command[0])) {
  19. int index=Integer.parseInt(command[1]);
  20. if(index>=0&&index<list.size()-1) {
  21. list.remove(index+1);
  22. }
  23. } else if("Swap".equals(command[0])) {
  24. if(list.contains(command[1]) && list.contains(command[2])) {
  25. int first=list.indexOf(command[1]);
  26. int second=list.indexOf(command[2]);
  27. Collections.swap(list,first,second);
  28. }
  29. } else if("Put".equals(command[0])) {
  30. int index=Integer.parseInt(command[2]);
  31. if(index>0 && index<list.size()+2) {
  32. list.add(index-1,command[1]);
  33. }
  34. } else if("Sort".equals(command[0])) {
  35. Collections.reverse(list);
  36. } else if("Replace".equals(command[0])) {
  37. if(list.contains(command[2])) {
  38. int index=list.indexOf(command[2]);
  39. list.set(index,command[1]);
  40. }
  41. }
  42. command=scanner.nextLine().split("\\s+");
  43. }
  44. for (int i = 0; i < list.size(); i++) {
  45. if(i==list.size()-1)
  46. {
  47. System.out.print(list.get(i));
  48. }else {
  49. System.out.print(list.get(i) + " ");
  50. }
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement