List<String[]> strsLst = new ArrayList<String[]>();
strsList的size不定。
其元素 String[] 的length 不定。
求这些数组之间的组合。
如:list中有 [1,2,3] [a,b] [101,102]
最终希望得到的完整结果是 :
1,a,101
1,a,102
1,b,101
1,b,102
2,a,101
2,a,102
2,b,101
2,b,102
3,a,101
3,a,102
3,b,101
3,b,102
其实是不同数组之间的组合。(~ o ~)~zZ 求帮助! 想了2个小时了,貌似陷入了逻辑误区!
求大神帮忙~~~~~~十分感谢!!!!
算法
strsList的size不定。
其元素 String[] 的length 不定。
求这些数组之间的组合。
如:list中有 [1,2,3] [a,b] [101,102]
最终希望得到的完整结果是 :
1,a,101
1,a,102
1,b,101
1,b,102
2,a,101
2,a,102
2,b,101
2,b,102
3,a,101
3,a,102
3,b,101
3,b,102
其实是不同数组之间的组合。(~ o ~)~zZ 求帮助! 想了2个小时了,貌似陷入了逻辑误区!
求大神帮忙~~~~~~十分感谢!!!!
算法
[1,2,3] [a,b] [101,102]
先将[1,2,3] [a,b]组合为["1,a","1,b","2,a","2,b","3,a","3,b"]再与[101,102]组合
package com.zf.test;import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;public class Test11 {
public Test11(){
compose(newStrs.toArray(new String[newStrs.size()]) , 0, new String[newStrs.size()], 0);
}
private List<String[]> strs = new ArrayList<String[]>();
//添加测试数据
{
strs.add(new String[]{"a" , "b" ,"c"});
strs.add(new String[]{"d" ,"e"});
strs.add(new String[]{"a" , "f"});
strs.add(new String[]{"h" , "i"});
}
private Set<String> newStrs = new HashSet<String>();
//合并数据
{
for (String[] ss : strs) {
newStrs.addAll(Arrays.asList(ss)) ;
}
}
//组合
public void compose(String[] array , int strsIndex , String[] tmpArray , int tmpIndex){
if(tmpIndex == array.length)
return ;
for (int i = strsIndex; i < array.length ; i++) {
tmpArray[tmpIndex] = array[i];
for (int j = 0; j <= tmpIndex; j++) {
System.out.print(tmpArray[j] + " ");
}
System.out.println();
compose(array ,i + 1 , tmpArray , tmpIndex + 1);
}
}
public static void main(String[] args) {
new Test11();
}
}
f
f d
f d e
f d e b
f d e b c
f d e b c a
f d e b c a h
f d e b c a h i
f d e b c a i
f d e b c h
f d e b c h i
f d e b c i
f d e b a
f d e b a h
f d e b a h i
f d e b a i
f d e b h
f d e b h i
f d e b i
f d e c
f d e c a
f d e c a h
f d e c a h i
f d e c a i
f d e c h
f d e c h i
f d e c i
f d e a
f d e a h
f d e a h i
f d e a i
f d e h
f d e h i
f d e i
f d b
f d b c
f d b c a
f d b c a h
f d b c a h i
f d b c a i
f d b c h
f d b c h i
f d b c i
f d b a
f d b a h
f d b a h i
f d b a i
f d b h
f d b h i
f d b i
f d c
f d c a
f d c a h
f d c a h i
f d c a i
f d c h
f d c h i
f d c i
f d a
f d a h
f d a h i
f d a i
f d h
f d h i
f d i
f e
f e b
f e b c
f e b c a
f e b c a h
f e b c a h i
f e b c a i
f e b c h
f e b c h i
f e b c i
f e b a
f e b a h
f e b a h i
f e b a i
f e b h
f e b h i
f e b i
f e c
f e c a
f e c a h
f e c a h i
f e c a i
f e c h
f e c h i
f e c i
f e a
f e a h
f e a h i
f e a i
f e h
f e h i
f e i
f b
f b c
f b c a
f b c a h
f b c a h i
f b c a i
f b c h
f b c h i
f b c i
f b a
f b a h
f b a h i
f b a i
f b h
f b h i
f b i
f c
f c a
f c a h
f c a h i
f c a i
f c h
f c h i
f c i
f a
f a h
f a h i
f a i
f h
f h i
f i
d
d e
d e b
d e b c
d e b c a
d e b c a h
d e b c a h i
d e b c a i
d e b c h
d e b c h i
d e b c i
d e b a
d e b a h
d e b a h i
d e b a i
d e b h
d e b h i
d e b i
d e c
d e c a
d e c a h
d e c a h i
d e c a i
d e c h
d e c h i
d e c i
d e a
d e a h
d e a h i
d e a i
d e h
d e h i
d e i
d b
d b c
d b c a
d b c a h
d b c a h i
d b c a i
d b c h
d b c h i
d b c i
d b a
d b a h
d b a h i
d b a i
d b h
d b h i
d b i
d c
d c a
d c a h
d c a h i
d c a i
d c h
d c h i
d c i
d a
d a h
d a h i
d a i
d h
d h i
d i
e
e b
e b c
e b c a
e b c a h
e b c a h i
e b c a i
e b c h
e b c h i
e b c i
e b a
e b a h
e b a h i
e b a i
e b h
e b h i
e b i
e c
e c a
e c a h
e c a h i
e c a i
e c h
e c h i
e c i
e a
e a h
e a h i
e a i
e h
e h i
e i
b
b c
b c a
b c a h
b c a h i
b c a i
b c h
b c h i
b c i
b a
b a h
b a h i
b a i
b h
b h i
b i
c
c a
c a h
c a h i
c a i
c h
c h i
c i
a
a h
a h i
a i
h
h i
i
public static List<String[]> getListArr(){
List<String[]> strArrList = new ArrayList<String[]>();
String[] strs1 = {"1","2","3"};
String[] strs2 = {"a","b"};
String[] strs3 = {"101","102"};
strArrList.add(strs1);
strArrList.add(strs2);
strArrList.add(strs3);
List<String[]> newList = new ArrayList<String[]>();
List<String[]> newList1 = new ArrayList<String[]>();
for(int i = 0 ; i < strArrList.get(0).length ; i++){
newList.add(new String[]{strArrList.get(0)[i]});
}
for(int i = 1 ; i < strArrList.size() ; i++){
String[] strs = strArrList.get(i);
for(int j = 0 ; j < newList.size() ; j++){
String newStr = "";
for(int ii = 0 ; ii < newList.get(j).length ; ii++){
newStr += newList.get(j)[ii];
}
for(int k = 0 ; k < strs.length ; k++){
String[] newStrs = {newStr + "," + strs[k]};
newList1.add(newStrs);
}
}
newList = newList1;
newList1 = new ArrayList<String[]>();
}
return newList;
}
十分感谢! 就是这种效果。
也有另外一个如下:public static List<String[]> getEndList(List<String[]> in){ List<String[]> out = new ArrayList<String[]>(); //输出list
int[] table = new int[in.size()];
int row = in.size()-1;
int m = 0;
do{
String[] o = new String[in.size()];
for(int x = table.length-1 ;x>=0;x--){
o[x] = in.get(x)[table[x]];
}
out.add(o);
m = table[row]+1;
//累加运算
if(m<in.get(row).length){
table[row] = m;
}else{
//进位运算
do{
//当前位归零
table[row]=0;
//升位
row--;
//判断是否超出位数
if(row<0){
return out;
}
m = table[row]+1;
//判断升为是否可以累加
if(m<in.get(row).length){
table[row] = m;
//累加后退出循环
row = in.size()-1;
}
}while(row!=in.size()-1);
}
}while(true);
}