Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class E02_ChangeList {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> list = parseLineToInts(scanner);
- String[] command = scanner.nextLine().split(" ");
- while(!command[0].equals("end")){
- int value = Integer.parseInt(command[1]);
- switch(command[0]){
- case "Delete":
- int i = 0;
- while(i < list.size()){
- if(list.get(i) == value){
- list.remove((Integer)value);
- } else {
- i++;
- }
- }
- break;
- case "Insert":
- int index = Integer.parseInt(command[2]);
- if(index > list.size()) {
- index = list.size();
- }
- list.add(index, value);
- break;
- }
- command = scanner.nextLine().split(" ");
- }
- for (Integer i : list) {
- System.out.print(i + " ");
- }
- }
- static List<Integer> parseLineToInts (Scanner scanner){
- String[] inputLine = scanner.nextLine().split(" ");
- List<Integer> inputList = new ArrayList<>();
- for (String s : inputLine) {
- inputList.add(Integer.parseInt(s));
- }
- return(inputList);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement