SHARE
TWEET

Change List

a guest Feb 17th, 2020 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class exercise {
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.  
  8.         String[] inputArr = sc.nextLine().split("\\s+");
  9.         ArrayList<String> numbers = new ArrayList<>();
  10.         for (String element : inputArr) {
  11.             numbers.add(element);
  12.         }
  13.         String input = sc.nextLine();
  14.         while (!input.equals("end")) {
  15.             String[] tokens = input.split("\\s+");
  16.             switch (tokens[0]) {
  17.                 case "Delete":
  18.                     while (numbers.contains(tokens[1])) {
  19.                         numbers.remove(tokens[1]);
  20.                     }
  21.                     break;
  22.                 case "Insert":
  23.                     String element = tokens[1];
  24.                     int index = Integer.parseInt(tokens[2]);
  25.                     if (index < numbers.size()) {
  26.                         numbers.add(index, element);
  27.                     }
  28.                     break;
  29.             }
  30.             input = sc.nextLine();
  31.         }
  32.  
  33.         System.out.print(String.join(" ", numbers));
  34.  
  35.     }
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top