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[] line = scanner.nextLine().split(" +");
- List<String> words = new ArrayList<>();
- for (int i = 0; i < line.length; i++) {
- words.add(line[i]);
- }
- String command = scanner.nextLine();
- while (!command.equals("Stop")) {
- String[] tokens = command.split(" +");
- String instruction = tokens[0];
- if (instruction.equals("Swap")) {
- String firstWord = tokens[1];
- int firstIndex = words.indexOf(firstWord);
- String secondWord = tokens[2];
- int secondIndex = words.indexOf(secondWord);
- if (firstIndex >= 0 && firstIndex < words.size()) {
- if (secondIndex >= 0 && secondIndex < words.size()) {
- Collections.swap(words, firstIndex, secondIndex);
- }
- }
- } else if (instruction.equals("Replace")) {
- String word1 = tokens[1];
- String word2 = tokens[2];
- int index2 = words.indexOf(word2);
- if (index2 >= 0 && index2 < words.size()) {
- words.set(index2, word1);
- }
- } else if (instruction.equals("Delete")) {
- int index = Integer.valueOf(tokens[1]);
- int indexToRemove = index + 1;
- if (indexToRemove >= 0 && indexToRemove < words.size()) {
- String wordToremove = words.get(indexToRemove);
- words.remove(wordToremove);
- }
- } else if (instruction.equals("Put")) {
- String wordToAdd = tokens[1];
- int indexToUse = Integer.parseInt(tokens[2]) - 1;
- if (indexToUse == words.size()) {
- words.add(wordToAdd);
- } else if (indexToUse >= 0 && indexToUse < words.size()) {
- words.add(indexToUse, wordToAdd);
- }
- } else if (instruction.equals("Sort")) {//descending order
- Collections.sort(words);
- Collections.reverse(words);
- }
- command = scanner.nextLine();
- }
- for (String word : words) {
- System.out.print(word + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement