功能:根据输入的字符串,将字符串中的所有值进行组装密码。
示例1:
String []a="0,1,2,3,4".split(",");
List<String> aList=new ArrayList<>();
List<String> bList=new ArrayList<>();
List<String> cList=new ArrayList<>();
List<String> dList=new ArrayList<>();
List<String> eList=new ArrayList<>();
for(int i=0;i<a.length;i++){
aList.add(a[i]);
for(int b=0;b<a.length;b++){
bList.add(a[i]+a[b]);
for(int c=0;c<a.length;c++){
cList.add(a[i]+a[b]+a[c]);
for(int d=0;d<a.length;d++){
dList.add(a[i]+a[b]+a[c]+a[d]);
for(int e=0;e<a.length;e++){
eList.add(a[i]+a[b]+a[c]+a[d]+a[e]);
}
}
}
}
}
System.out.println(aList.toString());//[0, 1, 2, 3, 4]
System.out.println(bList.toString());//[00, 01, 02, 03, 04, 10, 11, 12, 13, 14 太多省略.....]
System.out.println(cList.toString());//[000, 001, 002, 003, 004, 010, 011, 012, 013, 014, 020, 021 太多省略..]
System.out.println(dList.toString());//[0000, 0001, 0002, 0003, 0004, 0010, 0011, 0012, 0013, 0014, 0020太多省略...]
System.out.println(eList.toString());//[00000, 00001, 00002, 00003, 00004, 00010, 00011太多省略...]
System.out.println(aList.size()); //5
System.out.println(bList.size()); //25
System.out.println(cList.size()); //125
System.out.println(dList.size()); //625
System.out.println(eList.size()); //3125
改造后最终版:递归方式
要导依赖包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import lombok.Data;
import java.util.List;
/**
* 描述:
*
* @author lhh
* @Date 2020/7/18 9:08
*/
@Data
public class PwdUtils {
//密码最小长度
private Integer minPwdLength;
//密码最大长度
private Integer maxPwdLength;
//组装字符串 入参格式: "01234"
private String pwdCombine;
//密码数组,将入参的 "abc" 拆成 ["0","1","2","3","4"]
private String pwdStr[];
public PwdUtils(Integer minPwdLength, Integer maxPwdLength, String pwdCombine) {
this.minPwdLength = minPwdLength;
this.maxPwdLength = maxPwdLength;
this.pwdCombine = pwdCombine;
this.pwdStr = StrUtil.cut(this.pwdCombine, 1);
}
/**
* 生成密码
*/
public void genPwd() {
Multimap<Integer, String> m = ArrayListMultimap.create();
Integer c1 = 1;
for (int a = 0; a < this.pwdStr.length; a++) {
c1 = this.pwdStr[a].length();
m.put(c1, this.pwdStr[a]);
}
recursion(m, c1);
System.out.println(m.toString());
for (Integer s : m.keySet()) {
System.out.println(s + ":" + m.get(s).size());
}
}
/**
* 递归组装密码数据
*/
public void recursion(Multimap<Integer, String> m, Integer i) {
Integer c1 = i;
for (String s : this.pwdStr) {
List<String> strings1 = (List<String>) m.get(i);
for (String s1 : strings1) {
String c = s + s1;
c1 = c.length();
m.put(c.length(), c);
}
}
if (i < this.pwdStr.length - 1) {
recursion(m, c1);
}
}
public static void main(String[] args) {
PwdUtils r1 = new PwdUtils(1, 2, "01234");
r1.genPwd();
}
}
结论:得到的结果与示例1 一样,这样就不用考虑随着字符串密码的参数长度变化而去改变for的数量,字符串密码长度为5,那么我for就要有5个,如果字符串密码长度为10,for循环就要10个。使用递归方式解决这个for数量问题