Advertisement
Guest User

09. Combine Lists Of Letters

a guest
May 21st, 2014
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class _09_CombineListsOfLetters {
  6.  
  7.     public static void main(String[] args) {
  8.         try (Scanner scanner = new Scanner(System.in)) {
  9.             ArrayList<Character> list1 = new ArrayList<>();
  10.             ArrayList<Character> combinedList = new ArrayList<>();
  11.             // read chars from first list;
  12.             // with "replaceAll" method remove all whitespace
  13.             for (char c : scanner.nextLine().replaceAll("\\s+", "").toCharArray()) {
  14.                 list1.add(c);
  15.             }
  16.             combinedList.addAll(list1);
  17.             // add chars from second list to combined one,
  18.             // but only those chars, which do not exist in first list
  19.             for (char c : scanner.nextLine().replaceAll("\\s+", "").toCharArray()) {
  20.                 if (!list1.contains(c)) {
  21.                     combinedList.add(c);
  22.                 }
  23.                 // there's no need to save these chars
  24.             }
  25.             // print combined list
  26.             for (char c : combinedList) {
  27.                 System.out.print(c + " ");
  28.             }
  29.         } catch (Exception e) {
  30.             e.printStackTrace();
  31.         }
  32.  
  33.     }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement