LoraOrliGeo

MergingLists_Lists_Lab

Apr 8th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. package feb19_Lists_Lab;
  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 MergingLists {
  10.     public static void main(String[] args) {
  11.         @SuppressWarnings("resource")
  12.  
  13.         Scanner sc = new Scanner(System.in);
  14.        
  15.         List<Integer> first = Arrays.stream(sc.nextLine().split("\\s+")).map(Integer::parseInt).collect(Collectors.toList());
  16.         List<Integer> second = Arrays.stream(sc.nextLine().split("\\s+")).map(Integer::parseInt).collect(Collectors.toList());
  17.        
  18.         List<Integer> result = new ArrayList<>();
  19.        
  20.         int shorter = (first.size() <= second.size()) ? first.size() : second.size();
  21.        
  22.         for (int i = 0; i < shorter; i++) {
  23.             result.add(first.get(i));
  24.             result.add(second.get(i));
  25.         }
  26.        
  27.         if (first.size() > second.size()) {
  28.             result.addAll(getRemainElementsOfLongerLost(first, second));
  29.         } else if (second.size() > first.size()) {
  30.             result.addAll(getRemainElementsOfLongerLost(second, first));
  31.         }
  32.        
  33.         System.out.println(result.toString().replaceAll("\\[|,|\\]", ""));
  34.     }
  35.    
  36.     public static List<Integer> getRemainElementsOfLongerLost (List<Integer> first, List<Integer> second) {
  37.         List<Integer> result = new ArrayList<>();
  38.         for (int i = second.size(); i < first.size(); i++) {
  39.             result.add(first.get(i));
  40.         }
  41.         return result;
  42.     }
  43. }
Add Comment
Please, Sign In to add comment