desislava_topuzakova

07. Append Arrays

Oct 16th, 2021
671
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Lists_Exercise;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class AppendArrays_07 {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         String input = scanner.nextLine(); //"1 2 3 |4 5 6 |  7  8"
  13.         List<String> listSeparatedByPipe = Arrays.stream(input.split("\\|")).collect(Collectors.toList());
  14.         //"1 2 3 |4 5 6 |  7  8" -> split -> ["1 2 3 ", "4 5 6 ", "  7  8"] -> {"1 2 3 ", "4 5 6 ", "  7  8"}
  15.         Collections.reverse(listSeparatedByPipe);
  16.         // reverse ->  {"  7  8", "4 5 6 ", "1 2 3 "}
  17.  
  18.         //list.toString() -> "7 8 4 5 6 1 2 3"
  19.         System.out.println(listSeparatedByPipe.toString() //"[  7  8, 4 5 6 , 1 2 3 ]"
  20.                 .replace("[", "") //"  7  8, 4 5 6 , 1 2 3 ]"
  21.                 .replace("]", "") //"  7  8, 4 5 6 , 1 2 3 "
  22.                 .trim()  //"7  8, 4 5 6 , 1 2 3"
  23.                 .replaceAll(",", "") //"7  8 4 5 6  1 2 3"
  24.                 .replaceAll("\\s+", " ")); //"7 8 4 5 6 1 2 3"
  25.     }
  26. }
  27.  
RAW Paste Data