日期:2014-05-20 浏览次数:20894 次
public class TwoStringArraysGroups{
private String[] s1;
private String[] s2;
public TwoStringArraysGroups(String[] s1,String[] s2){
this.s1 = s1;
this.s2 = s2;
}
//本方法计算并返回两个数组的组合。
//
public String[] getGroups(){
String[] out = null;
if(s1 == null
------解决方案--------------------
s1.length == 0){
out = s2;
}else if( s2 == null
------解决方案--------------------
s2.length == 0){
out = s1;
}else{
int s2_length = s2.length; //数组2的长度.
int result_length = s1.length * s2.length; //结果数组的长度.
String[] result = new String[result_length]; //定义结果数组.
for(int i =0 ; i < result_length; i++){
result[i] = s1[ i/s2_length] + "-" +s2[ i % s2_length]; //关键就是这句。新数组第i 个元素与原来的两个数组的哪个元素有关。
}
out = result;
}
return out;
}
public static void main(String[] args){
String[] a = {"01","0102","010204"};
String[] b = {"02","0203","020305"};
TwoStringArraysGroups tsag = new TwoStringArraysGroups(a,b);
String[] c = tsag.getGroups();