简体   繁体   中英

Using an array in a recursive algorithm to find combinations

The idea is if i am at a certain stair i can either go one step down or two so if am at stair 3 i can go down 1 1 1 or 2 1 for example. My code should print all the possibilities. The error I get is that I can't convert the add function to an array (since the add method is a boolean). What is wrong with this algorithm?

public class Stairs {

public static void staircase (int height  ){

    ArrayList<Integer> Array = null;
    explore (height,Array);
}
public static void explore(int objheight,ArrayList<Integer>Array){
    int intialheight = 0; 
    if (intialheight == objheight){
        Array.toString();
    }
    else{ if (objheight > intialheight ){
        explore(objheight-2,Array.add(2));
        explore(objheight-1,Array.add(1));
    }
}

after your feedback I am getting an empty output import java.lang.reflect.Array; import java.util.ArrayList;

public class Stairs {

public static void staircase (int height  ){

    ArrayList<Integer> Array = new ArrayList<Integer>();
    explore (height,Array);
}
public static void explore(int objheight,ArrayList<Integer>Array){
    int intialheight = 0; 
    if (intialheight == objheight){
        Array.toString();
    }
    else{ if (objheight > intialheight ){
        Array.add(2);
        explore(objheight-2,Array);
        Array.add(1);
        explore(objheight-1,Array);
    }
}}
public static void main (String args[]){
staircase(3);

   }
 }

The method add(E e) in ArrayList returns true upon appending the element e passed as a parameter to the end of the ArrayList.

Your method, explore(int objHeight, ArrayList<Integer> Array) does not accept a boolean for its second parameter. Yet, in that same method, explore , you are recursively calling explore and passing in a boolean to the method.

The following code should be modified to first invoke the add method of Array and then pass Array to the explore method.

Before:

explore(objheight-2,Array.add(2)); This code is passing parameters int and boolean to the explore method, which is not the parameters it accepts. You should instead attempt the following.

After:

Array.add(2); explore(objheight-2,Array); This code first adds 2 to the Array and then passes the Array to the explore method without invoking any further methods on the Array object.


You will also need to do this for the next line of code, where you have explore(objheight-1,Array.add(1)); .


Edit: Upon further examination of the code, I discovered another (sooner) error that occurs. A NullPointerException will occur each time the program runs:

ArrayList<Integer> Array = null; explore (height,Array);

Then inside the explore method, different methods on Array are invoked, despite Array always being null :

Array.toString(); , Array.add(2) and Array.add(1) .

The Array object must be initialized inside of either the staircase or explore methods.

ArrayList<Integer> Array = new ArrayList<Integer>(); or ArrayList<Integer> Array = null; Array = new ArrayList<Integer>(); ArrayList<Integer> Array = null; Array = new ArrayList<Integer>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM