Advertisement
desislava_topuzakova

07. Append Arrays

Jun 19th, 2021
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         String input = scanner.nextLine(); //"1 2 3 |4 5 6 |  7  8"
  8.         List<String> list = Arrays.stream(input.split("\\|")).collect(Collectors.toList());
  9.         Collections.reverse(list);
  10.         //list.toString() ->
  11.         // "[  7  8, 4 5 6 , 1 2 3 ]"
  12.         // replace [ -> "  7  8, 4 5 6 , 1 2 3 ]"
  13.         // replace ] -> "  7  8, 4 5 6 , 1 2 3 "
  14.         // replace , -> "  7  8 4 5 6  1 2 3 "
  15.         //trim() -> "7  8 4 5 6  1 2 3"
  16.         //replace more than one interval -> "7 8 4 5 6 1 2 3"
  17.         System.out.println(list.toString().replace("[", "")
  18.                                             .replace("]", "")
  19.                                             .replaceAll(",", "")
  20.                                             .trim()
  21.                                             .replaceAll("\\s+", " "));
  22.  
  23.  
  24.     }
  25.  
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement