Advertisement
Guest User

ChangeList

a guest
Feb 19th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class ChangeList {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         String[] inputStringArray = sc.nextLine().split(" ");
  9.         List<Integer> integersList = stringArrayToIntList(inputStringArray);
  10.  
  11.         while (true) {
  12.             String[] action = sc.nextLine().split(" ");
  13.             if (action[0].equals("end")) {
  14.                 break;
  15.             } else if (action[0].equals("Delete")) {
  16.                 if (integersList.contains(Integer.parseInt(action[1]))) {
  17.                     for (int i = 0; i < integersList.size(); i++) {
  18.                         if (Integer.parseInt(action[1]) == integersList.get(i)) {
  19.                             integersList.remove(i);
  20.                             i = -1;
  21.                         }
  22.                     }
  23.                 }
  24.             } else if (action[0].equals("Insert")) {
  25.                 integersList.add(Integer.parseInt(action[2]), Integer.parseInt(action[1]));
  26.             }
  27.         }
  28.         printAListOfNumbers(integersList);
  29.     }
  30.  
  31.     public static void printAListOfNumbers(List<Integer> list) {
  32.         for (int i = 0; i < list.size(); i++) {
  33.             System.out.printf("%d ", list.get(i));
  34.         }
  35.     }
  36.  
  37.     public static List<Integer> stringArrayToIntList(String[] input) {
  38.         int[] inputToArray = new int[input.length];
  39.         for (int i = 0; i < inputToArray.length; i++) {
  40.             inputToArray[i] = Integer.parseInt(input[i]);
  41.         }
  42.  
  43.         List<Integer> inputList = new ArrayList<>();
  44.         for (int i = 0; i < inputToArray.length; i++) {
  45.             inputList.add(inputToArray[i]);
  46.         }
  47.         return inputList;
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement