简体   繁体   中英

I am not able to pass the values of one function to another function in java. How can I do it correctly ?

This is a code that I am working upon.

import java.io.*;
import java.util.*;    
public class Program {      
  public static void main(String[] args) {
      char[] a = new char[] {'0','1'};
      possibleStrings(3, a,"");
      char[] b = new char[] {'|','&'};
      possibleStrings(2, b,"");          
      result(a,b);   }

  public static void possibleStrings(int maxLength, char[] alphabet, String curr) {    
      if(curr.length() == maxLength) {
          System.out.print(curr);
          System.out.println();         
      } else {
          for(int i = 0; i < alphabet.length; i++) {
              String oldCurr = curr;
              curr += alphabet[i];
              possibleStrings(maxLength,alphabet,curr);
              curr = oldCurr;
          }}}

  public static void result(char[] arrayA,char[] arrayB) {     
        StringBuilder sb = new StringBuilder();
        int i = 0;
        while( i < arrayA.length && i < arrayB.length){
        sb.append(arrayA[i]).append(arrayB[i]);
        ++i;
        }
        for(int j = i; j < arrayA.length; ++j){
            sb.append(arrayA[j]);
        }
        for(int j = i; j < arrayB.length; ++j){
            sb.append(arrayB[j]);
        }            
        System.out.println("o/p="+sb.toString()); 
  }}

From main function I am passing the values to the possibleStrings and I am trying to collect the strings generated here back to the main function for both the same set of input ie possibleStrings(3, a,""); and possibleStrings(2, b,""); and then pass the strings a and b to the result function.

In result function I am trying to append the character strings character by character and obtain a combination of the output obtained in the above strings.

But the output that I am getting is wrong. I dont know where I am going wrong. Please help me out.

If i am correct here is a mistake while passing the data to function : possibleStrings(2, b,""); Replace it by : possibleStrings(2,'b',""); since it was a character i think it should be in single quote.

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