简体   繁体   中英

Call recursion method java

I have defined a recursion method (at least I believe it is recursive) that returns void and want to call it in another method, but don't know how. I know it's very basic, but can someone please help? Thanks.

Recursive method:

private static void recursiveWhiteToBlack(BufferedImage image, int width, int height){
    image.getRaster().setPixel(width,height, new int [] {0, 0, 0, 0, 0, 0});        
    int[][] neighbors = neighborsXY(width,height);

    for(int i = 0; i<neighbors.length; i++){
        int neighborX = neighbors[i][0];
        int neighborY = neighbors[i][1];
        int[] neighborColor = image.getRaster().getPixel(neighborX, neighborY, new int[] {0, 0, 0, 0, 0, 0});

        if(neighborColor[0] == 1){
            recursiveWhiteToBlack(image, neighborX, neighborY);
        }   
    }   
}

Calling it:

public static BufferedImage countObjects(BufferedImage image, BufferedImage original, ComponentPanel panel){
      BufferedImage target = copyImage(image);

      for(int width=1; width<image.getRaster().getWidth()-1; width++){ //Determine the dimensions for the width (x)         

          for(int height=1; height<image.getRaster().getHeight()-1; height++){ //Determine the dimensions for the height (y)

              int[] pixel = image.getRaster().getPixel(width, height, new int[] {0, 0, 0, 0, 0, 0});

              if(pixel[0] == 1){                      
                   none = recursiveWhitetoBlack(image, width, height);  //HOW TO CALL IT HERE!!!//

              }

      System.out.println("countObjects method called");
        return target;

    }   

You call it like this:

if(pixel[0] == 1){                      
     recursiveWhitetoBlack(image, width, height);
}

since the method has no return type, there is no need for variable assignment.

Remove none = since your method returns void (actually means it does not return anything)

So this should look like :

if(pixel[0] == 1){                      
    recursiveWhitetoBlack(image, width, height);  

}

also note that none is not defined as a variable/member so it is invalid to use it.

This could be trouble. I'm not sure you have a true stopping condition. You'll know right away when you get an out of memory error.

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