Didart

List Manipulation Basics

Oct 13th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class ListManipulationBasics {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.        
  10.         List<Integer> numList = Arrays
  11.                 .stream(scanner.nextLine().split(" "))
  12.                 .map(Integer::parseInt)
  13.                 .collect(Collectors.toList());
  14.  
  15.         String input = scanner.nextLine();
  16.         while (!input.equals("end")) {
  17.             List<String> commandLine = Arrays
  18.                     .stream(input.split(" "))
  19.                     .collect(Collectors.toList());
  20.  
  21.             String command = commandLine.get(0);
  22.  
  23.             switch (command) {
  24.                 case "Add":
  25.                     int numToAdd = Integer.parseInt(commandLine.get(1));
  26.                     numList.add(numToAdd);
  27.                     break;
  28.                 case "Remove":
  29.                     int numRemove = Integer.parseInt(commandLine.get(1));
  30.                     numList.remove(Integer.valueOf(numRemove));
  31.                     break;
  32.                 case "RemoveAt":
  33.                     int indexRemove = Integer.parseInt(commandLine.get(1));
  34.                     numList.remove(indexRemove);
  35.                     break;
  36.                 case "Insert":
  37.                     int numInsert = Integer.parseInt(commandLine.get(1));
  38.                     int indexInsert = Integer.parseInt(commandLine.get(2));
  39.  
  40.                     numList.add(indexInsert, numInsert);
  41.  
  42.                     break;
  43.             }
  44.             input = scanner.nextLine();
  45.         }
  46.         System.out.println(numList.toString()
  47.                 .replaceAll("[\\[\\],]", ""));
  48.     }
  49. }
  50.  
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment