Advertisement
vkarakolev

changeList

Nov 23rd, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class E02_ChangeList {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         List<Integer> list = parseLineToInts(scanner);
  9.  
  10.         String[] command = scanner.nextLine().split(" ");
  11.         while(!command[0].equals("end")){
  12.             int value = Integer.parseInt(command[1]);
  13.             switch(command[0]){
  14.                 case "Delete":
  15.                     int i = 0;
  16.  
  17.                     while(i < list.size()){
  18.                         if(list.get(i) == value){
  19.                             list.remove((Integer)value);
  20.                         } else {
  21.                             i++;
  22.                         }
  23.                     }
  24.                     break;
  25.                 case "Insert":
  26.                     int index = Integer.parseInt(command[2]);
  27.  
  28.                     if(index > list.size()) {
  29.                         index = list.size();
  30.                     }
  31.  
  32.                     list.add(index, value);
  33.                     break;
  34.              }
  35.  
  36.              command = scanner.nextLine().split(" ");
  37.          }
  38.  
  39.         for (Integer i : list) {
  40.             System.out.print(i + " ");
  41.         }
  42.     }
  43.  
  44.     static List<Integer> parseLineToInts (Scanner scanner){
  45.         String[] inputLine = scanner.nextLine().split(" ");
  46.         List<Integer> inputList = new ArrayList<>();
  47.  
  48.         for (String s : inputLine) {
  49.             inputList.add(Integer.parseInt(s));
  50.         }
  51.  
  52.         return(inputList);
  53.     }
  54. }
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement