实现字符串的所有组合(JAVA)

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

上一篇:CSS注释


下一篇:JVM08_字符串常量池基本特性、内存分配、拼接操作、intern、StringTable垃圾回收(二)