天天看点

参数的排列组合3

场景描述:

我有个测试同事,要测试一个接口,这个接口有多个参数,而且有的参数取值有多个.

比如参数method,可以取值["a","b","c"],并且个数不确定,即method,可以取值ab,也可是是abc

如何获取排列组合的所有值呢? 

之前咱们是求排列组合的取值个数,现在要求取值 

首先我们考虑常规的情况: 

我们有n个盒子,分别放置n个元素 

第一回:我们从n个里面选择一个:有n种可能 

第二回:我们从n-1个里面选择一个:有n-1种可能 

第三回:我们从n-2个里面选择一个:有n-2种可能 

第四回:我们从n-3个里面选择一个:有n-3种可能 

…… 

最后我们只有一个可选 

参数的排列组合3

 直接上代码:直接上代码:

参数的排列组合3

/*** 

    * @param base      :[a,b,c,d] 

    * @param times 

    * @param remaining : 剩余要选择的个数 

    * @return 

    */  

   public static void assemble(list<string> result, stringbuffer buffer, string base[], int times, int remaining, boolean issort) {  

       if (remaining <= 1) {  

           buffer.append(base[base.length - 1]);  

           if(times==0||times==base.length-remaining+1){  

            addelementbysort(result, buffer, issort);  

           }  

       } else {  

           for (int i = 0; i < remaining; i++) {  

               stringbuffer buffertmp = new stringbuffer(buffer);  

               buffertmp.append(base[base.length - 1 - i]);  

               if(times==0||times==base.length-remaining+1){  

                addelementbysort(result, buffertmp, issort);  

               }  

               assemble(result, buffertmp, systemhwutil.aheadelement(base, base.length - 1 - i), times, remaining - 1, issort);  

       }  

   }  

 参数说明:

参数名 含义 举例

base

样本

[a,b,c,d]

remaining

剩余可选择的样本个数

times

组合的个数

参数的排列组合3

工具类完整代码:

参数的排列组合3

package com.common.util;  

import com.string.widget.util.valuewidget;  

import java.util.arraylist;  

import java.util.list;  

/** 

 * created by huangweii on 2016/1/23. 

 */  

public class assembleutil {  

    /*** 

     * @param base      :[a,b,c,d] 

     * @param times 

     * @param remaining : 剩余要选择的个数 

     * @return 

     */  

    public static void assemble(list<string> result, stringbuffer buffer, string base[], int times, int remaining, boolean issort) {  

        if (remaining <= 1) {  

            buffer.append(base[base.length - 1]);  

            if(times==0||times==base.length-remaining+1){  

                addelementbysort(result, buffer, issort);  

            }  

        } else {  

            for (int i = 0; i < remaining; i++) {  

                stringbuffer buffertmp = new stringbuffer(buffer);  

                buffertmp.append(base[base.length - 1 - i]);  

                if(times==0||times==base.length-remaining+1){  

                    addelementbysort(result, buffertmp, issort);  

                }  

                assemble(result, buffertmp, systemhwutil.aheadelement(base, base.length - 1 - i), times, remaining - 1, issort);  

        }  

    }  

     * @param base 

     * @param issort    : 是否对"acb"进行排序,<br />排序结果:"abc" 

    public static list<string> assemble(string base[], int times,  boolean issort) {  

//        set<string> result = new hashset<string>();  

        list<string> result = new arraylist<string>();  

        assembleutil.assemble(result, new stringbuffer(), base, times, base.length, true);  

        return result;  

    public static void addelementbysort(list<string> result, stringbuffer buffer, boolean issort) {  

        string str = buffer.tostring();  

        if (issort) {  

            str = valuewidget.sortstr(str);  

        if (result.size() == 0 || (!result.contains(str))) {  

            result.add(str);  

     * 参数的取值个数,ab和ba算一种 

     * 

     * @param argcount 

    public static int getassemblesum(int argcount) {  

        int sum = 0;  

        for (int i = 0; i < argcount; i++) {  

            int count = i + 1;//参数组合的个数  

            sum += (systemhwutil.factorial(argcount, count) / systemhwutil.arrayarrange(count));  

        return sum;  

}