Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.crypto.Mac;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class ChangeList {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List listItems = Arrays
- .stream(scanner.nextLine().split(" "))
- .map(Integer::parseInt)
- .collect(Collectors.toList());
- String cmd = scanner.nextLine();
- while (!cmd.equalsIgnoreCase("end")) {
- String[] tokens = cmd.split(" ");
- int element = Integer.parseInt(tokens[1]);
- switch (tokens[0]) {
- case "Delete":
- //delete all elements in the array, which are equal to element
- listItems.removeAll(Collections.singleton(element));
- break;
- case "Insert":
- int position = Integer.parseInt(tokens[2]);
- if (position >= listItems.size() || position < 0) { //if position is valid index
- listItems.add(element);
- } else {
- listItems.add(position, element);
- }
- break;
- }
- cmd = scanner.nextLine();
- }
- listItems.forEach(e -> System.out.print(e + " "));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement