我想要实现两个集合中的元素配对,两个集合中的元素数相等,实现一对一配对,比如集合A1中有a,b,c三个元素,集合A2中有d,e,f三个元素,现在要求A1中元素和A2中元素配对的所有情况。如(a-d, d-e, c-f)是一种情况,(a-d, d-f, c-e)是另一种情况,请高手给出一个算法,输出所有这些情况。急求解法,谢谢!

解决方案 »

  1.   

    这个不就是叉乘么?既然要输出所有情况,也就没啥好优化的了
    for(Object a: setA){
        for(Object b: setB){
           output(a,b);
        }
    }妥啦!
      

  2.   

    楼上没有明白我的意思,a-d, d-e, c-f)是一种情况,是一个组合,我要的是所有组合情况,不是两个集合单个元素的配对
      

  3.   


    public void function(String str1[],String str2[]){
       foreach(String tmpstr1 in str1){
          foreach(String tmpstr2 in str2){
            System.out.println(tmpstr1+"-"+tmpstr2);
          }
       }
       return;
    }
      

  4.   

    哦这样啊:public static void main(String[] args) throws Exception {
            run1();    }    private static void run1() throws Exception {
            char data1[] = { 'a', 'b', 'c' };
            char data2[] = { 'x', 'y', 'z' };
            printResult("", data1, data2);    }    public static void printResult(String result, char data1[], char data2[]) {
            if (result.length() == data1.length * 4) {
                System.out.println(result);
                return;
            }
            for (int i = 0; i < data1.length; i++) {
                String dataA = String.valueOf(data1[i]);
                for (int j = 0; j < data2.length; j++) {                String dataB = String.valueOf(data2[j]);
                    if (result.indexOf(dataA) == -1 && result.indexOf(dataB) == -1) {
                        printResult(result + dataA + "-" + dataB + ",", data1,
                                data2);
                    }
                }
            }
        }
    输出结果:
    a-x,b-y,c-z,
    a-x,b-z,c-y,
    a-x,c-y,b-z,
    a-x,c-z,b-y,
    a-y,b-x,c-z,
    a-y,b-z,c-x,
    a-y,c-x,b-z,
    a-y,c-z,b-x,
    a-z,b-x,c-y,
    a-z,b-y,c-x,
    a-z,c-x,b-y,
    a-z,c-y,b-x,
    b-x,a-y,c-z,
    b-x,a-z,c-y,
    b-x,c-y,a-z,
    b-x,c-z,a-y,
    b-y,a-x,c-z,
    b-y,a-z,c-x,
    b-y,c-x,a-z,
    b-y,c-z,a-x,
    b-z,a-x,c-y,
    b-z,a-y,c-x,
    b-z,c-x,a-y,
    b-z,c-y,a-x,
    c-x,a-y,b-z,
    c-x,a-z,b-y,
    c-x,b-y,a-z,
    c-x,b-z,a-y,
    c-y,a-x,b-z,
    c-y,a-z,b-x,
    c-y,b-x,a-z,
    c-y,b-z,a-x,
    c-z,a-x,b-y,
    c-z,a-y,b-x,
    c-z,b-x,a-y,
    c-z,b-y,a-x,
      

  5.   

    结果集中的组合重复了,(a-x,b-y,c-z,)、(b-y,a-x,c-z,)、(b-y,c-z,a-x,)、(c-z,b-y,a-x,)、(c-z,a-x,b-y,)等这些组合重复了,只要一个就行了,还得优化一下
      

  6.   

    你得请客了,如果以下是对的:
    List<String> list1=new ArrayList<String>();
    List<String> list2=new ArrayList<String>();
    list1.add("a"); list1.add("b");list1.add("c");
    list2.add("d"); list2.add("e");list2.add("f");
    Set<String> set=new TreeSet<String>();//保证唯一性
    for(String l1:list1)
    for(String l2:list2)
    set.add(""+l1+"-"+l2);
    String[] str=new String[9];
    str=set.toArray(str);
    int n = list1.size();//列
    int m = str.length / n;//行
    String[][] strRes= new String[m][n];//转换数组
      int num = -1;   
    for (int i = 0; i < m; i++) 
    {
       for (int j = 0; j < n; j++) 
       {   num++;
       strRes[i][j] = str[num];    
       }
    }

    Set<String> res=new HashSet<String>();
    for (int i = 0; i < m; i++) 
    {
       for (int j = 0; j < n; j++) 
       {  
        for(int r=i+1;r<m;r++)//note
        {    
        for(int s=0;s<n;s++)
        {
        if(s==j)continue;
        for(int u=r+1;u<m;u++)//note,集合中的数越多算法复杂
        {
        for(int v=0;v<n;v++)
        {
        if(v==s|v==j)continue;
        System.out.println("" +strRes[i][j]+" "+strRes[r][s]+
        " "+strRes[u][v]);    
     }        
        }    
        }
        }
    }    
    }