Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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. */
- import java.util.ArrayList;
- import java.util.Scanner;
- public class _09_CombineListsOfLetters {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter sequence of letters to the First list on single line, separated by a space:");
- String firstLine = scan.nextLine();
- System.out.println("Enter sequence of letters to the Second list on single line, separated by a space:");
- String secondLine = scan.nextLine();
- String[] firstArr = firstLine.split("[^a-zA-Z]");
- String[] secondArr = secondLine.split("[^a-zA-Z]");
- ArrayList<Character> combinedList = new ArrayList<>();
- ArrayList<Character> secondList = new ArrayList<>();
- for (int i = 0; i < firstArr.length; i++) {
- char letter = firstArr[i].charAt(0);
- combinedList.add(letter);
- }
- for (int i = 0; i < secondArr.length; i++) {
- char letter = secondArr[i].charAt(0);
- if (!combinedList.contains(letter)) {
- secondList.add(letter);
- }
- }
- combinedList.addAll(secondList);
- System.out.println("The obtained combined list is:\n" + combinedList);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement