/
_09_CombineListsOfLetters.java
59 lines (52 loc) · 2.11 KB
/
_09_CombineListsOfLetters.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Problem 9. Combine Lists of Letters
Write a program that reads two lists of letters l1 and l2 and combines them:
appends all letters c from l2 to the end of l1, but only when c does not appear in l1.
Print the obtained combined list.
All lists are given as sequence of letters separated by a single space,
each at a separate line. Use ArrayList<Character> of chars to keep the input and output lists.
*/
package homework_collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class _09_CombineListsOfLetters {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//left letters(first input line)
char[] arrayLeft = input.nextLine().toCharArray();
ArrayList<Character> leftList = new ArrayList<>();
//right letters(second input line)
char[] arrayRight = input.nextLine().toCharArray();
ArrayList<Character> rightList = new ArrayList<>();
//add char values from char[] in to ArrayList of Character
addCharsToList(arrayLeft, leftList);
addCharsToList(arrayRight, rightList);
// list for the result
ArrayList<Character> finalCombination = new ArrayList<>();
addCharsToList(arrayLeft, finalCombination);
boolean alreadyHave = false;
for (int i = 0; i < rightList.size(); i++) {
Character currentChar = rightList.get(i);
alreadyHave = false;
for (int j = 0; j < leftList.size(); j++) {
if (currentChar.equals(leftList.get(j))) {
alreadyHave = true;
break;
}
}
if (!alreadyHave) {
finalCombination.add(currentChar);
}
}
//Print result
for (int i = 0; i < finalCombination.size(); i++) {
System.out.printf("%s ", finalCombination.get(i));
}
}
private static void addCharsToList(char[] arrayLeft, ArrayList<Character> leftList) {
for (int i = 0; i < arrayLeft.length; i += 2) {
leftList.add(arrayLeft[i]);
}
}
}