/**
*
*编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,
* 例如: 原始字符串是"abc",打印得到下列所有组合情况: "a" "b" "c"
* "ab" "bc" "ca" "ba" "cb" "ac"
* "abc" "acb" "bac" "bca" "cab" "cba"
*
* @author Administrator
*
*/
public class Text6 {
public static void main(String[] args) {
char[] ch = { 'a', 'b', 'c' };
ArrayList list = new ArrayList();
ArrayList list2 = new ArrayList();
ArrayList list3 = new ArrayList();
for (int i = 0; i < ch.length; i++) {
list.add(ch[i]);
for (int j = 0; j < ch.length; j++) {
if (i != j) {
list2.add(ch[i] + "" + ch[j] + " ");
for (int j2 = 0; j2 < ch.length; j2++) {
if (j2 != i && j2 != j) {
list3.add(ch[i] + "" + ch[j] + "" + ch[j2]);
}
}
}
}
}
for (int a = 0; a < list.size(); a++) {
System.out.print(list.get(a) + " ");
}
//下面会出错。
for (int a = 0; a < list2.size(); a++) {
System.out.print(list.get(a) + " ");
}
for (int a = 0; a < list3.size(); a++) {
System.out.print(list.get(a) + " ");
}
}
}
这个方法可以么?我想把for循环遍历封装,但是总报错,求大神帮忙。
|
|