Question

In: Computer Science

JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c]...

JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution

[ [a,b,c] , [d,e], [f] ] ----> [ [a,d,f], [a,e,f], [b,d,f], [b,e,f], [c,d,f], [c,e,f]]

[ [a,b], [a,b,c]] ----->[[a,a],[a,b],[a,c], [b,a],[b,b],[b,c]

assuming abc are integer

Thanks in advance!

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Arrays;

public class IntCombinations {

        private static ArrayList<ArrayList<Integer>> mergeTwoLists(ArrayList<Integer> partList,
                        ArrayList<ArrayList<Integer>> overallList) {
                ArrayList<ArrayList<Integer>> result = new ArrayList<>();

                for (Integer x : partList) {
                        for (ArrayList<Integer> y : overallList) {
                                ArrayList<Integer> l2Copy = new ArrayList<>(y);
                                l2Copy.add(0, x);
                                result.add(l2Copy);
                        }
                }

                return result;
        }

        public static ArrayList<ArrayList<Integer>> generateCombs(ArrayList<ArrayList<Integer>> lists) {
                ArrayList<ArrayList<Integer>> result = new ArrayList<>();
                result.add(new ArrayList<>());
                
                for(ArrayList<Integer> x: lists) {
                        result = mergeTwoLists(x, result);
                }
                
                return result;
        }

        public static void main(String[] args) {
                ArrayList<Integer> x1 = new ArrayList<>(Arrays.asList(1, 2, 3));
                ArrayList<Integer> x2 = new ArrayList<>(Arrays.asList(4, 5));
                ArrayList<Integer> x3 = new ArrayList<>(Arrays.asList(6));
                
                ArrayList<ArrayList<Integer>> result = generateCombs(new ArrayList<ArrayList<Integer>>(Arrays.asList(x1, x2, x3)));

                for(ArrayList<Integer> x: result) {
                        System.out.println(x);
                }
                
        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm =...
JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm = new HashMap<Integer,Integer>(); for (int i= 0; i<2; i++) { al.add(i); al.add(i+1); hs.add(i); hs.add(i+1); hm.put(al.get(i), al.get(i+1)); hm.put(hm.get(i), hm.get(i+1)); }   System.out.println(al); System.out.println(hs); System.out.println(hm); // {key=value}. ---------------------------------- What output is produced by the following code and why?
how to use java ArrayList to get the result? removing consecutive duplicates eg [1 2 2...
how to use java ArrayList to get the result? removing consecutive duplicates eg [1 2 2 3 2 2 1] ------->[1 2 3 2 1]
(Java Programming Problem) Build two ArrayList lists from the Integer and String type arrays, populate the...
(Java Programming Problem) Build two ArrayList lists from the Integer and String type arrays, populate the lists with repeats (see example below). Write a generic method (removeDuplicates( ….. )) to remove those duplicate items and returns an ArrayList<E> list without duplicates. Original Integer List: [14, 24, 14, 42, 24, 25, 25, 23] No-Duplicate List: [14, 24, 42, 25, 23] Same generic method for the name list Original List: [Mike, Lara, Jenny, Lara, Jared, Jonny, Lindsey, Mike, Jared] No-Duplicate List: [Mike,...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and returns the expanded ArrayList.  The total size will be (k+1) * n.   public ArrayList<Integer> makeCopies(ArrayList<Integer>, int k) { } Example: ArrayList<Integer> has (3,7,4) and k = 2, then the returned, expanded ArrayList will have (3,7,4,3,7,4,3,7,4).  The total size is (k+1)*n = (2+1)*3= 9.
Java OVERVIEW This program primarily focuses on the implementation of a ArrayList type interface and the...
Java OVERVIEW This program primarily focuses on the implementation of a ArrayList type interface and the necessary methods to implement the ArrayList. It also includes polymorphism and class comparison. INSTRUCTIONS Your deliverable will be to turn in three files. The files will be named Card.java, PremiumCard.java and the last file will be called CardArrayList.java. For this assignment, any use of a data control structure other than a simple Arrary or String will result in no credit. I am aware that...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that integer squared, ending with newline. (Note: This assignment is configured to have students programming directly in the zyBook. Instructors may instead require students to upload a file). Below is a program that's been nearly completed for you. Click "Run program". The output is wrong. Sometimes a program lacking input will produce wrong output (as in this case), or no output. Remember to always pre-enter...
(JAVA) Repeated Class diagram: Repeated + repeatedDetector(ArrayList<Integer> a) : int For this exercise, you will have...
(JAVA) Repeated Class diagram: Repeated + repeatedDetector(ArrayList<Integer> a) : int For this exercise, you will have to implement the class diagram above. Basically, the objective of this exercise is to develop the class and the previous method, in such a way that it tells us the amount of "repeated" elements that are in the dynamic array. When we speak of "repeated", we mean counting the number of numbers of the same value that are found in succession. Example 1: The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT