【列出一个字符串的全字符组合】

来源:互联网 发布:bt天堂软件下载 编辑:程序博客网 时间:2024/05/17 03:00
编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符
例如:
原始字符串是"abc",打印得到下列所有组合情况
"a" "b" "c" 
"ab" "bc" "ca" "ba" "cb" "ac"
"abc" "acb" "bac" "bca" "cab" "cba"

import java.util.ArrayList;import java.util.List;public class Test2 {public static void main(String[] args) {String [] strs={"a","b","c"};List<String>oldList=new ArrayList<String>();List<String>newList=new ArrayList<String>();for(int i=1;i<=strs.length;i++){if(oldList.size()==0 ){for(String str:strs){if(myMothod(str)){newList.add(str);System.out.print(str+" ");}}System.out.println();}else{for(String str1:oldList){for(String str2:strs){if(myMothod(str1+str2)){newList.add(str1+str2);System.out.print(str1+str2+" ");}}}System.out.println();}//请空旧的容器,将新容器的数据导入旧的容器oldList.clear();for(String str:newList){oldList.add(str);}newList.clear();}}//定义一个判断字符串中是否有重复的制定的字符的方法public static boolean myMothod(String str){for(int i=0;i<str.length();i++){char c=str.charAt(i);for(int j=i+1;j<str.length();j++){if(c==str.charAt(j))return false;}}return true;}}


0 0
原创粉丝点击