- package com.itheima;
- import java.util.ArrayList;
- public class Test5 {
- /**
- * 5、 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,例如:
-
- 原始字符串是"abc",打印得到下列所有组合情况:
- "a" "b" "c"
- "ab" "bc" "ca" "ba" "cb" "ac"
- "abc" "acb" "bac" "bca" "cab" "cba"
- * @param args
- */
- public static void main(String[] args) {
- String str = "abcd";
- char[] ch = str.toCharArray();
- ArrayList<Character> al = new ArrayList<Character>();
- subComb(ch, al);
- }
- public static void subComb(char[] ch,ArrayList<Character> al){
-
- for(int i =0; i<ch.length;i++){
- if(al.contains(ch[i]))
- continue;
- al.add(al.size(),ch[i]);
- System.out.println(charALtoString(al));
- if(al.size()==ch.length){
- al.remove(al.size()-1);
- break;
- }
- subComb(ch, al);
- al.remove(al.size()-1);
- }
-
- }
- private static String charALtoString(ArrayList<Character> al) {
- // TODO 自动生成的方法存根
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < al.size(); i++) {
- sb.append(al.get(i));
- }
- return sb.toString();
- }
- }
复制代码 |
|