public class Combine { public static void main(String[] args) { new Combine().combine(new String("STJ")); } void combine(String str) { char[] in = str.toCharArray(); StringBuffer out = new StringBuffer(); allCombine(in, out, 0); } void allCombine(char[] in, StringBuffer out, int start) { for (int i = start; i < in.length; i++) { out.append(in[i]); System.out.println(out); if (i < in.length - 1) { //如果有下个元素,则递归折行 allCombine(in, out, i + 1); } out.setLength(out.length() - 1); //清空out } } }
输出结果:
S
ST
STJ
SJ
T
TJ
J
参见:http://topic.csdn.net/u/20081215/19/a72991ed-5801-4223-8705-81e2d66ca8c5.html