Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads in two set of positive Integers. One set goes into an

ID: 3857857 • Letter: W

Question

Write a program that reads in two set of positive Integers. One set goes into an ArrayList int1 and second set goes into another ArrayList int2. Create a third arrayList of Integers that contains the shared Integers from int1 and int2 (i.e, the ones that are common to both ArrayLists). Order does not matter. A sample dialog is shown below: Enter the first set of integers on one line, end with -1 10 200 6 99 3 5 90 44 -1 Enter the second set of integers on one line, end with -1 200 56 34 3 5 87 44 5 1000 -1 Array List with shared Integers: [200, 3, 5, 44]

Explanation / Answer

ArrayListCommonTest.java

import java.util.ArrayList;

import java.util.Scanner;

public class ArrayListCommonTest {

public static void main(String[] args) {

ArrayList<Integer> int1 = new ArrayList<Integer>();

ArrayList<Integer> int2 = new ArrayList<Integer>();

ArrayList<Integer> int3 = new ArrayList<Integer>();

Scanner scan = new Scanner(System.in);

System.out.println("Enter the first set of integers on one line, end with -1: ");

Integer s = scan.nextInt();

while(s!=-1){

int1.add(s);

s = scan.nextInt();

}

System.out.println("Enter the second set of integers on one line, end with -1: ");

s = scan.nextInt();

while(s!=-1){

int2.add(s);

s = scan.nextInt();

}

for(int i=0; i<int1.size(); i++){

if(int2.contains(int1.get(i))){

int3.add(int1.get(i));

}

}

System.out.println("ArrayList with shared Integers: ");

System.out.println(int3);

}

}

Output:

Enter the first set of integers on one line, end with -1:
10 200 6 99 3 5 90 44 -1
Enter the second set of integers on one line, end with -1:
200 56 34 3 5 87 44 5 1000 -1
ArrayList with shared Integers:
[200, 3, 5, 44]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote