- /**
- * 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,例如: 原始字符串是"abc",打印得到下列所有组合情况: "a" "b" "c" "ab"
- * "bc" "ca" "ba" "cb" "ac" "abc" "acb" "bac" "bca" "cab" "cba"
- */
- public class CharacterAssemble {
- public static void main(String[] args) throws IOException {
- System.out.println("请输入一个字符串:");
- BufferedReader bufferedReader = new BufferedReader(
- new InputStreamReader(System.in));
- String str = bufferedReader.readLine().trim();
- String[] strs = str.split("");
- List<String> temp = new ArrayList<String>();
- List<String> list = new ArrayList<String>();
- List<String> listAll = new ArrayList<String>();
- int len = strs.length;
- for (int i = 0; i < len; i++) {
- for (int j = 0; j < len; j++) {
- if (!temp.isEmpty()) {
- for (int k = 0; k < temp.size(); k++) {
- // 判断是否有重复
- if (!temp.get(k).contains(strs[j]))
- list.add(temp.get(k) + strs[j]);
- }
- } else {
- list.add(strs[j]);
- }
- }
- listAll.addAll(list);
- // 将list的元素复制给temp
- temp.addAll(list);
- // list清空
- list.clear();
- }
- System.out.println(listAll);
- }
- }
复制代码 |
|