Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class AppendArrays {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine(); //example input: "1 2 3 |4 5 6 | 7 8"
- List<String> list = Arrays.stream(input.split("\\|")).collect(Collectors.toList());
- Collections.reverse(list); //list elements are all symbols between the |
- // => reverse will reverse the whole elements, not the separate numbers
- // => the output is [ 7 8, 4 5 6 , 1 2 3 ]
- System.out.println(list.toString().replaceAll("[\\[\\],]", "")
- .trim().replaceAll("\\s+", " "));
- //list.toString() -> "[ 7 8, 4 5 6 , 1 2 3 ]"
- // replace [ -> " 7 8, 4 5 6 , 1 2 3 ]"
- // replace ] -> " 7 8, 4 5 6 , 1 2 3 "
- // replace , -> " 7 8 4 5 6 1 2 3 "
- //trim() -> "7 8 4 5 6 1 2 3"
- //replace more than one interval -> "7 8 4 5 6 1 2 3"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment