Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package _15_Lists;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class _03_MergingLists {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> list1 = Arrays.stream(scanner.nextLine().split(" "))
- .map(Integer::parseInt).collect(Collectors.toList());
- List<Integer> list2 = Arrays.stream(scanner.nextLine().split(" "))
- .map(Integer::parseInt).collect(Collectors.toList());
- List<Integer> listResult = new ArrayList<>();
- int smallestSize = Math.min(list1.size(), list2.size());
- for (int i = 0, j = 0; i < smallestSize * 2; i++) {
- if (i % 2 == 0 || i == 0) {
- listResult.add(i, list1.get(j));
- } else if (i % 2 == 1) {
- listResult.add(i, list2.get(j));
- j++;
- }
- }
- if (list1.size() > list2.size()) {
- listResult.addAll(getRemainingElements(list1, list2));
- } else if (list2.size() > list1.size()) {
- listResult.addAll(getRemainingElements(list2, list1));
- }
- System.out.println(listResult.toString().replaceAll("[\\[\\],]",""));
- }
- public static List<Integer> getRemainingElements
- (List<Integer> longerList, List<Integer> shorterList) {
- List<Integer> nums = new ArrayList<>();
- for (int i = shorterList.size(); i < longerList.size(); i++)
- nums.add(longerList.get(i));
- return nums;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement