日期:2014-05-20  浏览次数:20683 次

求一算法!
给定若干数组,注意是若干(数量不定)
new String[] { "张","章","脏" }
new String[] { "三", "山", "删" }
new String[] { "疯", "峰", "风", "封" }
-----------------------------------------------------
得到如下结果:
张三疯,张三峰,张三风,张三封,张山疯,张山峰,张山风......

求过路朋友指点
算法

------解决方案--------------------
import java.util.ArrayList;
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();
}
}