Ask Question
17 March, 09:35

Write a method that returns the union of two arrays lists of integers using the following header:

Public static ArrayList union (Array list, ArrayList list2)

For example, the addition of two array lists (2,3,1,5) and (3,4,6) is (2,3,1,5,3,4,6).

Write a test program that prompts the user to enter two lists, each with five integers, and displays their union. The number are separated by exactly one space. Here is a sample run:

Enter five integers for list1: 3 5 45 4 3

Enter five integers for list2: 33 51 5 4 13

The combined list is 3 5 45 4 3 33 51 5 4 13

+4
Answers (1)
  1. 17 March, 10:09
    0
    import java. util.*;

    public class Main

    {

    public static void main (String[] args) {

    Scanner input = new Scanner (System. in);

    ArrayList l1 = new ArrayList (5);

    ArrayList l2 = new ArrayList (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 union (ArrayList list, ArrayList 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
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a method that returns the union of two arrays lists of integers using the following header: Public static ArrayList union (Array ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers