问题描述
- java排列组合出所有的结果
-
代码:List<String[]> list = new ArrayList<String[]>(); String[] str = new String[1]; str[0] = "a1"; list.add(str); str = new String[3]; str[0] = ",b1"; str[1] = ",b2"; str[2] = ",b3"; list.add(str); str = new String[2]; str[0] = ",c1"; str[1] = ",c2"; list.add(str); str = new String[2]; str[0] = ",e1"; str[1] = ",e2"; list.add(str); str = new String[4]; str[0] = ",d1"; str[1] = ",d2"; str[2] = ",d3"; str[3] = ",d4"; list.add(str);
我想的到所有组合的结果,不过每个数据的下标不能变
就好比:- 1. a1,b1,c2,d1,e1
- 1. a1,b1,c2,d1,e1
- 1. a1,b1,c2,d1,e1
- 1.
- 1. a1,b2,c2,d1,e1
- 1. a1,b2,c2,d1,e1
- 1. a1,b2,c2,d1,e1
- 1.
- 1. a1,b3,c2,d1,e1
- 1. a1,b3,c2,d1,e1
- 1. a1,b3,c2,d1,e1
- 1.
- 1. a1,b1,c1,d2,e1
- 1. a1,b1,c1,d2,e1
- 1. a1,b1,c1,d2,e1
- 1.
- 1. a1,b2,c1,d2,e1
- 1. a1,b2,c1,d2,e1
- 1. a1,b2,c1,d2,e1
- 1.
- 1. a1,b3,c1,d2,e1
- 1. a1,b3,c1,d2,e1
- 1. a1,b3,c1,d2,e1
- 1.
- 1. a1,b1,c2,d2,e1
- 1. a1,b1,c2,d2,e1
- 1. a1,b1,c2,d2,e1
- 1.
- 1. a1,b2,c2,d2,e1
- 1. a1,b2,c2,d2,e1
- 1. a1,b2,c2,d2,e1
- 1.
- 1. a1,b3,c2,d2,e1
- 1. a1,b3,c2,d2,e1
- 1. a1,b3,c2,d2,e1
- 1.
- 1. a1,b1,c3,d2,e1
- 1. a1,b1,c3,d2,e1
- 1. a1,b1,c3,d2,e1
- 1.
- 1. a1,b2,c3,d2,e1
- 1. a1,b2,c3,d2,e1
- 1. a1,b2,c3,d2,e1
- 1.
- 1. a1,b3,c3,d2,e1
- 1. a1,b3,c3,d2,e1
- 1. a1,b3,c3,d2,e1
....
怎么用Java循环出来啊
解决方案
java排列组合
----------------------
解决方案二:
不知道你的1是什么意思
public void listString(list,index){
if(index == list.size()){
System.out.println();
}
String[] s = list.get(index);
for(int i = 0; i < s.length(); i++){
if(index == 0){
System.out.print(s[i]);
}
else{
System.out.print("," + s[i]);
}
listString(list,index+1);
}
}
解决方案三:
import java.util.*;
public class FullPermutation
{
public static void main(String[] args)
{
System.out.println("Hello World!");
List<String[]> list = new ArrayList<String[]>();
String[] str = new String[1];
str[0] = "a1";
list.add(str);
str = new String[3];
str[0] = "b1";
str[1] = "b2";
str[2] = "b3";
list.add(str);
str = new String[2];
str[0] = "c1";
str[1] = "c2";
list.add(str);
str = new String[2];
str[0] = "e1";
str[1] = "e2";
list.add(str);
str = new String[4];
str[0] = "d1";
str[1] = "d2";
str[2] = "d3";
str[3] = "d4";
list.add(str);
for(String[] s : list){
System.out.println(Arrays.toString(s));
}
System.out.println("--------------");
fullPermutation(list,0,null);
}
public static void fullPermutation(List<String[]> list,int index,List<String> result){
if(index == list.size()){
System.out.println(result);
return;
}
String[] s = list.get(index);
for(int i = 0; i < s.length; i++){
if(index == 0){
result = new ArrayList<String>();
result.add(s[i]);
}
else{
result.add(s[i]);
}
fullPermutation(list,index + 1,result);
result.remove(s[i]);
}
}
}
时间: 2024-11-09 19:40:55