Given the definition of a 2D array such as the following Str

Given the definition of a 2D array such as the following String data = {{\"A\", \"B\"}, {\"1\", \"2\"}, {\"XX\", \"YY\", \"ZZ\"}}; write a recursive program that outputs all combinations of each subarray in order. In the above example, the desired output (although it doesn\'t have to be in this order) is: A 1 XXA 1 YYA 1 ZZA 2 XXA 2 YYA 2 ZZB 1 XXB 1 YYB 1 ZZB 2 XX3 2 YYB 2 ZZ Your program should work with arbitrarily sized arrays in either dimension. For example, the following data String data = {{\"A\"}, {\"1\"}, {\"2\"}, {\"XX\", \"YY\"}}; should output: A 1 2 XXA 1 2 YY

Solution

Import java.util.arraylist;

Public class combinations

{

Public state void main (string [] args)

{

Array list<string>output=new array list <string> ();

String [] [] data1= {

     {“A”,”B”},

     {“1”,”2”},

     {“X”,”Y”,”Z”}

     };

String [] [] Data2={

    {“A”},

    {“1”},

    {“2”},

    {“XX”,”YY”}

    };

System .out.println (“output result for input data1 [] [] array);

Permute (data1, 0, ouput);

System.out.println (“output result for input data2 [] [] array”);

Permute (data2, 0, output);

}

Public static void permute (string array [][] ,int index, array list<string>output)

{

If (index==array. Length)

System.out.println (output.tostring ());

Else

{

For (int i=0;i<array[index].length ;i++)

{

Output. Add (array [index][i]);

Permute (array, index+1, output);

Output. Remove (output. size ()-1);

}

}

}

}

 Given the definition of a 2D array such as the following String data = {{\
 Given the definition of a 2D array such as the following String data = {{\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site