Answer:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Integer> l1 = new ArrayList<Integer>(5);
ArrayList<Integer> l2 = new ArrayList<Integer>(5);
System.out.println("Enter five integers for list1: ");
for (int i=0; i<5; i++) {
int x = input.nextInt();
l1.add(x);
}
System.out.println("Enter five integers for list2: ");
for (int i=0; i<5; i++) {
int x = input.nextInt();
l2.add(x);
}
System.out.println(union(l1, l2));
}
public static ArrayList<Integer> union(ArrayList<Integer> list, ArrayList<Integer> list2) {
for (int i:list2)
list.add(i);
return list;
}
}
Explanation:
Create a method called union takes two lists, list and list2
Inside the method:
Initialize a for loop iterates through the list2
Add all the elements in list2 to list
Return the list
Inside the main:
Declare the lists
Ask the user for the numbers and put them in the lists
Call the union method to combine the lists and print the combined list