SvetlanPetrova

AppendArrays SoftUni

Jun 29th, 2021
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class AppendArrays {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String input = scanner.nextLine(); //example input: "1 2 3 |4 5 6 |  7  8"
  12.         List<String> list = Arrays.stream(input.split("\\|")).collect(Collectors.toList());
  13.         Collections.reverse(list); //list elements are all symbols between the |
  14.         // => reverse will reverse the whole elements, not the separate numbers
  15.         // => the output is [  7  8, 4 5 6 , 1 2 3 ]
  16.  
  17.         System.out.println(list.toString().replaceAll("[\\[\\],]", "")
  18.                 .trim().replaceAll("\\s+", " "));
  19.         //list.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.         // replace , -> "  7  8 4 5 6  1 2 3 "
  23.         //trim() -> "7  8 4 5 6  1 2 3"
  24.         //replace more than one interval -> "7 8 4 5 6 1 2 3"
  25.     }
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment