java字符串组合排列递归问题

String a0,a1,a2,...,an;
a0="我饿全文";
a1="ABC";
a2="abc";
''''
an="wre";
每次从每个an中只取一个字符组合成字符串,全部排列出来,如:
我+A+a+w,我+A+a+r等。这个怎么弄啊?

5个回答

谢谢,跟我要的还是有点不一样。

 /* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String[] arr = new String[3];
        arr[0] = "abc";
        arr[1] = "123";
        arr[2] = "xyz";
        String[] arr1 = new String[1];
        arr1[0] = "";
        String[] arr2 = arr1;
        for (int i = 0; i < arr.length; i++)
        {
            arr2 = new String[arr1.length * arr[i].length()];
            for (int j = 0; j < arr2.length; j++)
            {
                arr2[j] = arr1[j / arr[i].length()] + String.valueOf(arr[i].charAt(j % arr[i].length()));
            }
            arr1 = arr2;
        }
        for (String s : arr2)
            System.out.println(s);
    }
}

http://ideone.com/V0t44J

 a1x
a1y
a1z
a2x
a2y
a2z
a3x
a3y
a3z
b1x
b1y
b1z
b2x
b2y
b2z
b3x
b3y
b3z
c1x
c1y
c1z
c2x
c2y
c2z
c3x
c3y
c3z

反正我不喜欢递归,也没有必要递归。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐