Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class _09_CombineListsOfLetters {
- public static void main(String[] args) {
- try (Scanner scanner = new Scanner(System.in)) {
- ArrayList<Character> list1 = new ArrayList<>();
- ArrayList<Character> combinedList = new ArrayList<>();
- // read chars from first list;
- // with "replaceAll" method remove all whitespace
- for (char c : scanner.nextLine().replaceAll("\\s+", "").toCharArray()) {
- list1.add(c);
- }
- combinedList.addAll(list1);
- // add chars from second list to combined one,
- // but only those chars, which do not exist in first list
- for (char c : scanner.nextLine().replaceAll("\\s+", "").toCharArray()) {
- if (!list1.contains(c)) {
- combinedList.add(c);
- }
- // there's no need to save these chars
- }
- // print combined list
- for (char c : combinedList) {
- System.out.print(c + " ");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement