Advertisement
deyanmalinov

02. Change List

Feb 28th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. package com.company;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4. public class Main {
  5.         public static void main(String[] args) {
  6.             Scanner scan = new Scanner(System.in);
  7.  
  8.             List <String> strList = Arrays.stream(scan.nextLine().split(" ")).collect(Collectors.toList());
  9.  
  10.             String line = scan.nextLine();
  11.             while (!line.equals("end")){
  12.  
  13.                 String [] comTwo = line.split(" ");
  14.                 String cmd = comTwo[0];
  15.                 line = scan.nextLine();
  16.                 if (cmd.equals("Delete")){
  17.                     String  elem = comTwo[1];
  18.                     while (strList.contains(elem)){
  19.                         strList.remove(elem);
  20.                     }
  21.                 }else {
  22.                     String elem = comTwo[1];
  23.                     int index = Integer.parseInt(comTwo[2]);
  24.                     if (index<strList.size()) {
  25.                         strList.add(index, elem);
  26.                     }
  27.                 }
  28.  
  29.  
  30.             }
  31.  
  32.             System.out.println(strList.toString().replaceAll("[\\[\\],]", ""));
  33.         }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement