Ask Question
19 May, 05:27

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

+2
Answers (1)
  1. 19 May, 07:36
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers