output a list of concatenated strings

c0rnfl4k3s :

I'm trying to implement a method that uses multiple String[] arrays to print a wordlist which contains every possible combination of the Strings in the arrays, in order and using max. 1 String of each array.

example: {"this" | "that"} {"is" | "was"} {"cool" | "lame"}

String[] array1 = {"this", "that"};
String[] array2 = {"is", "was"};
String[] array3 = {"cool", "lame"};

should be used for the following output:

thisiscool
thatiscool
thiswascool
thatwascool
thiswaslame
thatwaslame
thisislame
thatislame

I've been experimenting with nested for-loops:

String out1 = "";
for(int a = 0; a < array1.length; a++) {
            out1 = array1[a];
            System.out.println(out1);
            for(int b = 0; b < array2.length; b++) {
                out1 += array2[b];
                System.out.println(out1);
                for(int c = 0; c < array3.length; c++) {
                    out1 += array3[c];
                    System.out.println(out1);

This didn't work well though. Maybe there's a better approach to this?

J4BEZ :

@Supercool. has already left a perfect answer.
But as your opinion, I've been trying to figure out if there's a better way to visualize than nested for-loops, and I've come up with a way to "recursive call"

By using a 'double array' and 'recursive',
although you add a new array, you don't have to write an additional for-loop.

Like this

public class StackOver
{
    static String[][] array = {{"this","that"},
                              {"is","was"},
                              {"cool","lame"},};

    public static void recString(String[][] a, int index, String output) {
        //make break-point
        if(index == a.length) {             //if 'index' reached to the end of array 'a'?
            System.out.println(output);     //print out string 'output' that collected so far
            //output should be = (a[0][?] + a[1][?] +...+ a[length-1][?])
            return;                         //And end the method.
        }

        // if 'index' didn't reach to the end of array :: 
        //If there's an array that hasn't been explored yet,
        for(int i = 0; i < a[index].length; i++) {
            recString(a, index+1, output + a[index][i]);
            //Add 1 to 'index' and add String out put that added '[index][i]' 
            //as parameters and call this method again.

            //This form is called recursive call!
        }
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String t = "";

        recString(array,0,t);
    }
 }

enter image description here

Even if the array changes, with recursive calls, you can examine every array without changing the code and draw possible combinations.

Ex).

static String[][] array = {{"I","You","They"},
                               {"love","hate"},
                               {"me","you"},
                               {"too","either"},};

Well, the grammar is a little awkward, but it's an example of stretching the arrangement a little bit longer.

enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=349529&siteId=1