Advertisement
veronikaaa86

03. Merging Lists

Oct 13th, 2021
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. package lists;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class P03MergingLists {
  10. public static void main(String[] args) {
  11. Scanner scanner = new Scanner(System.in);
  12.  
  13. List<Integer> firstList = Arrays
  14. .stream(scanner.nextLine().split(" "))
  15. .map(Integer::parseInt)
  16. .collect(Collectors.toList());
  17.  
  18. List<Integer> secondList = Arrays
  19. .stream(scanner.nextLine().split(" "))
  20. .map(Integer::parseInt)
  21. .collect(Collectors.toList());
  22.  
  23. List<Integer> resultList = new ArrayList<>();
  24.  
  25. int minSize = Math.min(firstList.size(), secondList.size());
  26.  
  27. for (int i = 0; i < minSize; i++) {
  28. int firstItem = firstList.get(i);
  29. int secondItem = secondList.get(i);
  30.  
  31. resultList.add(firstItem);
  32. resultList.add(secondItem);
  33. }
  34.  
  35. if (firstList.size() > secondList.size()) {
  36. resultList.addAll(firstList.subList(minSize, firstList.size()));
  37. } else {
  38. resultList.addAll(secondList.subList(minSize, secondList.size()));
  39. }
  40.  
  41. System.out.println(resultList.toString().replaceAll("[\\[\\],]", ""));
  42. }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement