How to get 2D array possible combinations

前端 未结 5 2053
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-17 20:18

I have the following 2D array:

String[M][]

String[0]
   \"1\",\"2\",\"3\"

String[1]
   \"A\", \"B\"
   .
   .
   .
String[M-1]
   \"!\"

A

5条回答
  •  粉色の甜心
    2020-12-17 20:45

    I have been struggling with this problem for some time. But I finally solved it. My main obstacle was the SCOPE I used for declaring each variable. If you do not declare your variables in the correct scope, then the variable will retain changes made in the previous iteration.

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class RecursiveAlgorithmTest {
        private static int recursiveCallsCounter = 0;
        public static ArrayList> testCases = new ArrayList>();
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            //set values for ArrayOfArrays
            ArrayList VariableA = new ArrayList(Arrays.asList("red", "green"));
            ArrayList VariableB = new ArrayList(Arrays.asList("A", "B", "C"));
            ArrayList VariableC = new ArrayList(Arrays.asList("1", "2", "3", "4"));
    
            ArrayList> AofA = new ArrayList>();
            AofA.add(VariableA); AofA.add(VariableB); AofA.add(VariableC);
    
            System.out.println("Array of Arrays: ToString(): " +AofA.toString());
    
            ArrayList optionsList = new ArrayList();
    
            //recursive call
            recurse(optionsList, AofA, 0);
    
            for (int i = 0 ; i < testCases.size() ; i++) {
                System.out.println("Test Case " + (i+1) + ": " + testCases.get(i));
                }
    
            }//end main(String args[])
    
    
    
        private static void recurse(ArrayList newOptionsList, 
            ArrayList> newAofA, int placeHolder){
            recursiveCallsCounter++;
            System.out.println("\n\tStart of Recursive Call: " + recursiveCallsCounter);
            System.out.println("\tOptionsList: " + newOptionsList.toString());
            System.out.println("\tAofA: " + newAofA.toString());
            System.out.println("\tPlaceHolder: "+ placeHolder);
    
            //check to see if we are at the end of all TestAspects
            if(placeHolder < newAofA.size()){
    
                //remove the first item in the ArrayOfArrays
                ArrayList currentAspectsOptions = newAofA.get(placeHolder);
                //iterate through the popped off options
    
    
    
    
                for (int i=0 ; i newOptions = new ArrayList();
                    //add all the passed in options to the new object to pass on
                    for (int j=0 ; j < newOptionsList.size();j++) {
                        newOptions.add(newOptionsList.get(j));
                    }
    
                    newOptions.add(currentAspectsOptions.get(i));
                    int newPlaceHolder = placeHolder + 1;
                    recurse(newOptions,newAofA, newPlaceHolder);
                }
            } else { // no more arrays to pop off
                ArrayList newTestCase = new ArrayList();
                for (int i=0; i < newOptionsList.size();i++){
                    newTestCase.add(newOptionsList.get(i));
                }
                System.out.println("\t### Adding: "+newTestCase.toString());
                testCases.add(newTestCase);
            }
        }//end recursive helper 
    }// end of test class
    

提交回复
热议问题