给定若干数组,注意是若干(数量不定)
new String[] { "张","章","脏" }
new String[] { "三", "山", "删" }
new String[] { "疯", "峰", "风", "封" }
-----------------------------------------------------
得到如下结果:
张三疯,张三峰,张三风,张三封,张山疯,张山峰,张山风......求过路朋友指点算法
new String[] { "张","章","脏" }
new String[] { "三", "山", "删" }
new String[] { "疯", "峰", "风", "封" }
-----------------------------------------------------
得到如下结果:
张三疯,张三峰,张三风,张三封,张山疯,张山峰,张山风......求过路朋友指点算法
String[] s2 = new String[] { "三", "山", "删" };
String[] s3 = new String[] { "疯", "峰", "风", "封" };
for(int i =0; i<s1.length; i++){
for(int j=0; j<s2.length; j++){
for(int k=0; k<s3.length; k++){
System.out.println(s1[i]+s2[j]+s3[k]);
}
}
}
我说的是若干数组,数量不定!
import java.util.Arrays;
import java.util.List;public class Test20130326 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
compose(new String[]{"A","B","C"},new String[]{"a","b","c"},new String[]{"1","2"});
}
public static void compose(Object ...obj){
int start = 0;
//int wLength = -1;
int hLength = obj.length;
for(Object arr:obj){
if(!(arr instanceof String[])){
System.out.println("The type is not right!");
return;
}
}
//wLength = ((String[])obj[hLength-1]).length;
StringBuilder s = new StringBuilder("");
getComposeString(s,(String[])obj[0],start,hLength,obj);
}
public static String getComposeString(StringBuilder str,String[] obj,int index,int maxSize,Object ...all){
//String[] temp = new String[obj.length] ;
//System.arraycopy(obj, 0, temp, 0, obj.length);
String temp = str.toString();
for(int i=0;i<obj.length;i++){
str = new StringBuilder(temp);
if(index==(maxSize-1)){
str.append(obj[i]);
//str.append(obj[i]);
System.out.println(str);
if(i ==obj.length-1){
return temp;
}
}else{
str.append(obj[i]);
str.append( getComposeString(str,(String[])all[index+1],index+1,maxSize,all));
}
}
return str.toString();
}
}