Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] strings=scanner.nextLine().split("\\s+");
- List<String> list=new ArrayList<>();
- for (String string:strings) {
- list.add(string);
- }
- String[] command=scanner.nextLine().split("\\s+");
- while (!"Stop".equals(command[0])) {
- if("Delete".equals(command[0])) {
- int index=Integer.parseInt(command[1]);
- if(index>=0&&index<list.size()-1) {
- list.remove(index+1);
- }
- } else if("Swap".equals(command[0])) {
- if(list.contains(command[1]) && list.contains(command[2])) {
- int first=list.indexOf(command[1]);
- int second=list.indexOf(command[2]);
- Collections.swap(list,first,second);
- }
- } else if("Put".equals(command[0])) {
- int index=Integer.parseInt(command[2]);
- if(index>0 && index<list.size()+2) {
- list.add(index-1,command[1]);
- }
- } else if("Sort".equals(command[0])) {
- Collections.reverse(list);
- } else if("Replace".equals(command[0])) {
- if(list.contains(command[2])) {
- int index=list.indexOf(command[2]);
- list.set(index,command[1]);
- }
- }
- command=scanner.nextLine().split("\\s+");
- }
- for (int i = 0; i < list.size(); i++) {
- if(i==list.size()-1)
- {
- System.out.print(list.get(i));
- }else {
- System.out.print(list.get(i) + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement