Question 1: Write a program that reads words into two ArrayLists list1 and list2
ID: 3844692 • Letter: Q
Question
Question 1: Write a program that reads words into two ArrayLists list1 and list2 and then creates a third ArrayList that contains words which are common to both list1 and list2. You may assume that the strings are entered as words separated by a space on a single line and the end is signaled by “-1” (String -1). You can use keyboard.next() to read each word.
Question 2: Write a program that reads words into an ArrayList list1 and creates another ArrayList list2 that has the same words in list1 but no duplicates.
Exercise 1: Write a program that reads words into two ArrayLists list 1 and list2 and then creates a third Array List that contains words which are common to both list1 and list2. You may assume that the strings are entered as words separated by a space on a single line and the end is signaled by "-1" (String -1). You can use keyboard.next0 to read each word. A sample dialog is shown below Enter words on one line, end with -1 java c pascal ada java c++ -1 Enter words on one line, end with -1 c pascal java lisp isp -1 java c, pascal ada java, C++] CC, pascal java, lisp, lisp Array List with common strings: [c, pascal java Exercise 2: Write a program that reads words into an ArrayList list1 and creates another ArrayList list2 that has the same words in list1 but no duplicates. A sample dialog is shown below: Enter words on one line, end with -1 java c pascal ada java java ada C++ -1 Array List with no duplicates java C, pascal ada c++]Explanation / Answer
Question 1:
ArrayListTester.java
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListTester {
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
ArrayList<String> list3 = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
System.out.println("Enter words on one line, end with -1: ");
String s = scan.nextLine();
Scanner scan1 = new Scanner(s);
while(scan1.hasNext()){
String word = scan1.next();
if(!word.equals("-1")){
list1.add(word);
}
}
System.out.println("Enter words on one line, end with -1: ");
s = scan.nextLine();
scan1 = new Scanner(s);
while(scan1.hasNext()){
String word = scan1.next();
if(!word.equals("-1")){
list2.add(word);
}
}
for(int i=0; i<list1.size(); i++){
for(int j=0; j<list2.size(); j++){
if(list1.get(i).equalsIgnoreCase(list2.get(j))){
if(!list3.contains(list1.get(i))){
list3.add(list1.get(i));
}
}
}
}
System.out.println(list1);
System.out.println(list2);
System.out.println("ArrayList with common strings: ");
System.out.println(list3);
}
}
Output:
Enter words on one line, end with -1:
java c pascel ada java c++ -1
Enter words on one line, end with -1:
c pascel java lisp lisp -1
[java, c, pascel, ada, java, c++]
[c, pascel, java, lisp, lisp]
ArrayList with common strings:
[java, c, pascel]
Question 2:
ArrayListTester2.java
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListTester2 {
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
System.out.println("Enter words on one line, end with -1: ");
String s = scan.nextLine();
Scanner scan1 = new Scanner(s);
while(scan1.hasNext()){
String word = scan1.next();
if(!word.equals("-1")){
list1.add(word);
}
}
for(int i=0; i<list1.size(); i++){
if(!list2.contains(list1.get(i))){
list2.add(list1.get(i));
}
}
System.out.println(list1);
System.out.println("ArrayList with no duplicates: ");
System.out.println(list2);
}
}
Output:
Enter words on one line, end with -1:
java c pascel ada java java ada c++ -1
[java, c, pascel, ada, java, java, ada, c++]
ArrayList with no duplicates:
[java, c, pascel, ada, c++]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.