Called method output differs from Caller

My God

This program checks every combination of inputArray and prints it -

Checker.java -

package testpj;

import java.util.ArrayList;

public class Checker {
public static void main(String[] args) {
    String[] subarrayA = {"A0","A1","A2"};

    String[] subarrayB = {"B0","B1"};
    String[] subarrayC = {"C0","C1","C2","C3"};
    String[][] inputArray = {subarrayA, subarrayB, subarrayC};
    ArrayList<String> output = new ArrayList<String>();
    String output1 = permute(inputArray, 0, output);
    System.out.println("the output1: "+output1);
}

public static String permute(String array[][], int index, ArrayList<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); 
        }
    }
    return output.toString();
}
}

OUTPUT:

[A0, B0, C0]
[A0, B0, C1]
[A0, B0, C2]
[A0, B0, C3]
[A0, B1, C0]
[A0, B1, C1]
[A0, B1, C2]
[A0, B1, C3]
[A1, B0, C0]
[A1, B0, C1]
[A1, B0, C2]
[A1, B0, C3]
[A1, B1, C0]
[A1, B1, C1]
[A1, B1, C2]
[A1, B1, C3]
[A2, B0, C0]
[A2, B0, C1]
[A2, B0, C2]
[A2, B0, C3]
[A2, B1, C0]
[A2, B1, C1]
[A2, B1, C2]
[A2, B1, C3]
the output1: []

Why is output1 contains no element ?

My God

The correct answer is when I call permute recursively then it re-initializes the value of output again and so we end up having no value in the output variable. I added one more variable to the permute method and added the output to it instead of adding the output to output itself and it worked.

Solution:

 public ArrayList<String> permute(String array[][], 
 int index, ArrayList<String> output, ArrayList<String> comb){
        if(index == array.length){
            comb.add(output.toString());            
        }
        else{
            for(int i=0 ; i<array[index].length ; i++){
                output.add(array[index][i]);
                permute(array,index+1,output, comb);
                output.remove(output.size() - 1); 
            }
        }

        return comb;
    }

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Better Practice: Printing from void method OR returning value from method and printing from method caller

분류에서Dev

Is there a way to access the variables/fetch the assigned values of the caller class in a method of the called class?

분류에서Dev

ouput in brower differs from output in terminal char>128 python3 apache2

분류에서Dev

Dynamically call a method from a dynamically called class

분류에서Dev

ls output differs stdout vs screen

분류에서Dev

Does new transaction starts when method b() is called from method a()?

분류에서Dev

C++ check from where specified function/method was called

분류에서Dev

The build file called using <ant> task resets the logging configs of the caller

분류에서Dev

iptables resolved addresses differs from nslookup

분류에서Dev

Calling a Method if another Method is called

분류에서Dev

When is toString() method called

분류에서Dev

A layman's explanation for "Everything is a file" — what differs from Windows?

분류에서Dev

How to use a method from the Startup form to enable its controls when called through a child form?

분류에서Dev

In Java is there a name for the object the method is called on?

분류에서Dev

AbstractTableModel setValueAt method not being called

분류에서Dev

Control method called using variables

분류에서Dev

Should a method called by a constructor be static?

분류에서Dev

Should a method called by a constructor be static?

분류에서Dev

How to stub a method that is called in initialize method

분류에서Dev

How to output info text from class method invoked via artisan command in Laravel 4

분류에서Dev

how can i modify main method to get input and output from the comandline?

분류에서Dev

jQuery+Edge Animate: Error only when called from click event: Object [object Object] has no method 'foundation'

분류에서Dev

Application onCreate method gets called twice

분류에서Dev

Flutter: Geolocator return the method 'compareTo' was called on null

분류에서Dev

why is mockito not called when executing mocked method?

분류에서Dev

NSInvocation & NSTimer - Method gets called twice

분류에서Dev

Broadcast Receiver "on recieve" method not been called?

분류에서Dev

shouldPerformSegueWithIdentifier called before checking other method issue

분류에서Dev

Get called method name without __call

Related 관련 기사

  1. 1

    Better Practice: Printing from void method OR returning value from method and printing from method caller

  2. 2

    Is there a way to access the variables/fetch the assigned values of the caller class in a method of the called class?

  3. 3

    ouput in brower differs from output in terminal char>128 python3 apache2

  4. 4

    Dynamically call a method from a dynamically called class

  5. 5

    ls output differs stdout vs screen

  6. 6

    Does new transaction starts when method b() is called from method a()?

  7. 7

    C++ check from where specified function/method was called

  8. 8

    The build file called using <ant> task resets the logging configs of the caller

  9. 9

    iptables resolved addresses differs from nslookup

  10. 10

    Calling a Method if another Method is called

  11. 11

    When is toString() method called

  12. 12

    A layman's explanation for "Everything is a file" — what differs from Windows?

  13. 13

    How to use a method from the Startup form to enable its controls when called through a child form?

  14. 14

    In Java is there a name for the object the method is called on?

  15. 15

    AbstractTableModel setValueAt method not being called

  16. 16

    Control method called using variables

  17. 17

    Should a method called by a constructor be static?

  18. 18

    Should a method called by a constructor be static?

  19. 19

    How to stub a method that is called in initialize method

  20. 20

    How to output info text from class method invoked via artisan command in Laravel 4

  21. 21

    how can i modify main method to get input and output from the comandline?

  22. 22

    jQuery+Edge Animate: Error only when called from click event: Object [object Object] has no method 'foundation'

  23. 23

    Application onCreate method gets called twice

  24. 24

    Flutter: Geolocator return the method 'compareTo' was called on null

  25. 25

    why is mockito not called when executing mocked method?

  26. 26

    NSInvocation & NSTimer - Method gets called twice

  27. 27

    Broadcast Receiver "on recieve" method not been called?

  28. 28

    shouldPerformSegueWithIdentifier called before checking other method issue

  29. 29

    Get called method name without __call

뜨겁다태그

보관